sensors: k2hh: Fix accelerometer
authorDario Trombello <dariotr@outlook.de>
Thu, 20 Feb 2020 18:41:57 +0000 (18:41 +0000)
committerDario Trombello <dariotr@outlook.de>
Sat, 28 Mar 2020 21:38:30 +0000 (22:38 +0100)
Using the STMicroelectronics K2HH driver from SM-J700F Android 6.0 kernel source (J700FXXU4BQE3) makes the sensor work.

Change-Id: I9f50ea5096b56617b171d5cc64c2ed1b01a3e205

drivers/sensors/k2hh.c

index 193a74498811bdf6c013bf72176a497eaf4928e3..197d5788039683465780a17b566cd22ddbaa1ca0 100644 (file)
@@ -27,7 +27,7 @@
 #include <linux/of_gpio.h>
 #include <linux/wakelock.h>
 #include <linux/regulator/consumer.h>
-
+#include <linux/interrupt.h>
 #include <linux/sensor/sensors_core.h>
 
 #define I2C_M_WR                      0 /* for i2c Write */
 #define CHIP_ID_RETRIES               3
 #define ACCEL_LOG_TIME                15 /* 15 sec */
 
+#define K2HH_TOP_UPPER_RIGHT          0
+#define K2HH_TOP_LOWER_RIGHT          1
+#define K2HH_TOP_LOWER_LEFT           2
+#define K2HH_TOP_UPPER_LEFT           3
+#define K2HH_BOTTOM_UPPER_RIGHT       4
+#define K2HH_BOTTOM_LOWER_RIGHT       5
+#define K2HH_BOTTOM_LOWER_LEFT        6
+#define K2HH_BOTTOM_UPPER_LEFT        7
+
 #define K2HH_MODE_SUSPEND             0
 #define K2HH_MODE_NORMAL              1
 
 #define CTRL1_BDU_MASK                0x08
 
 /* CTRL2 */
-#define CTRL2_DFC_MASK                0x60
-#define CTRL2_DFC_50                  0x00
-#define CTRL2_DFC_100                 0x20
-#define CTRL2_DFC_9                   0x40
-#define CTRL2_DFC_400                 0x60
+#define CTRL2_IG1_INT1                0x08
 
 /* CTRL3 */
 #define CTRL3_IG1_INT1                0x08
 #define K2HH_ACC_BW_100               0x80
 #define K2HH_ACC_BW_200               0x40
 #define K2HH_ACC_BW_400               0x00
-
 #define INT_THSX1_REG                 0x32
 #define INT_THSY1_REG                 0x33
 #define INT_THSZ1_REG                 0x34
-
 #define K2HH_ACC_BW_SCALE_ODR_ENABLE  0x08
 #define K2HH_ACC_BW_SCALE_ODR_DISABLE 0x00
 
 #define DYNAMIC_THRESHOLD             5000
 
-#define ENABLE_LPF_CUT_OFF_FREQ              1
-#define ENABLE_LOG_ACCEL_MAX_OUT      1
-#if defined(ENABLE_LOG_ACCEL_MAX_OUT)
-#define ACCEL_MAX_OUTPUT              32760
-#endif
-
 enum {
        OFF = 0,
        ON = 1
@@ -159,7 +156,7 @@ struct k2hh_p {
        struct mutex mode_mutex;
        struct hrtimer accel_timer;
        struct workqueue_struct *accel_wq;
-       struct work_struct work_accel;
+       struct work_struct work;
        struct regulator *reg_vio;
 #ifdef CONFIG_SENSORS_K2HH_VDD
        struct regulator *reg_vdd;
@@ -171,6 +168,8 @@ struct k2hh_p {
        int irq1;
        int irq_state;
        int acc_int1;
+       int sda_gpio;
+       int scl_gpio;
        int time_count;
 
        u8 odr;
@@ -183,17 +182,18 @@ struct k2hh_p {
        u8 negate_x;
        u8 negate_y;
        u8 negate_z;
-
        u64 old_timestamp;
+       const char *str_vdd;
+       const char *str_vio;
 };
 
-#define ACC_ODR10     0x10     /*   10Hz output data rate */
-#define ACC_ODR50     0x20     /*   50Hz output data rate */
-#define ACC_ODR100    0x30     /*  100Hz output data rate */
-#define ACC_ODR200    0x40     /*  200Hz output data rate */
-#define ACC_ODR400    0x50     /*  400Hz output data rate */
-#define ACC_ODR800    0x60     /*  800Hz output data rate */
-#define ACC_ODR_MASK  0X70
+#define ACC_ODR10              0x10    /*   10Hz output data rate */
+#define ACC_ODR50              0x20    /*   50Hz output data rate */
+#define ACC_ODR100             0x30    /*  100Hz output data rate */
+#define ACC_ODR200             0x40    /*  200Hz output data rate */
+#define ACC_ODR400             0x50    /*  400Hz output data rate */
+#define ACC_ODR800             0x60    /*  800Hz output data rate */
+#define ACC_ODR_MASK           0X70
 
 struct k2hh_acc_odr {
        unsigned int cutoff_ms;
@@ -213,8 +213,11 @@ const struct k2hh_acc_odr k2hh_acc_odr_table[] = {
 #endif
 };
 
+#ifdef CONFIG_SENSORS_LDO_CONTROL
 extern unsigned int lpcharge;
+#endif
 
+static int k2hh_regulator_onoff(struct k2hh_p *data, bool onoff);
 static int k2hh_i2c_read(struct k2hh_p *data,
                unsigned char reg_addr, unsigned char *buf, unsigned int len)
 {
@@ -238,7 +241,7 @@ static int k2hh_i2c_read(struct k2hh_p *data,
        } while (retries++ < 2);
 
        if (ret < 0) {
-               SENSOR_ERR("i2c read error %d\n", ret);
+               SENSOR_ERR(" i2c read error %x, %d\n", data->client->addr, ret);
                return ret;
        }
 
@@ -267,7 +270,7 @@ static int k2hh_i2c_write(struct k2hh_p *data,
        } while (retries++ < 2);
 
        if (ret < 0) {
-               SENSOR_ERR("i2c write error %d\n", ret);
+               SENSOR_ERR(" i2c write error %d\n", ret);
                return ret;
        }
 
@@ -280,7 +283,8 @@ static int k2hh_read_accel_xyz(struct k2hh_p *data, struct k2hh_v *acc)
        struct k2hh_v rawdata;
        unsigned char buf[READ_DATA_LENTH];
 
-       ret = k2hh_i2c_read(data, AXISDATA_REG, buf, READ_DATA_LENTH);
+       ret += k2hh_i2c_read(data, AXISDATA_REG, buf, READ_DATA_LENTH);
+
        if (ret < 0)
                goto exit;
 
@@ -288,12 +292,12 @@ static int k2hh_read_accel_xyz(struct k2hh_p *data, struct k2hh_v *acc)
        rawdata.v[1] = ((s16) ((buf[3] << 8) | buf[2]));
        rawdata.v[2] = ((s16) ((buf[5] << 8) | buf[4]));
 
-       acc->v[0] = ((data->negate_x) ? (-rawdata.v[data->axis_map_x]) :
-               (rawdata.v[data->axis_map_x]));
-       acc->v[1] = ((data->negate_y) ? (-rawdata.v[data->axis_map_y]) :
-               (rawdata.v[data->axis_map_y]));
-       acc->v[2] = ((data->negate_z) ? (-rawdata.v[data->axis_map_z]) :
-               (rawdata.v[data->axis_map_z]));
+       acc->v[0] = ((data->negate_x) ? (-rawdata.v[data->axis_map_x])
+                  : (rawdata.v[data->axis_map_x]));
+       acc->v[1] = ((data->negate_y) ? (-rawdata.v[data->axis_map_y])
+                  : (rawdata.v[data->axis_map_y]));
+       acc->v[2] = ((data->negate_z) ? (-rawdata.v[data->axis_map_z])
+                  : (rawdata.v[data->axis_map_z]));
 
 exit:
        return ret;
@@ -357,16 +361,6 @@ static int k2hh_set_odr(struct k2hh_p *data)
        data->odr = new_odr;
 
        SENSOR_INFO("change odr %d\n", i);
-
-#if defined(ENABLE_LPF_CUT_OFF_FREQ)
-       /* To increase LPF cut-off frequency, ODR/DFC */
-       k2hh_i2c_read(data, CTRL2_REG, &buf, 1);
-
-       buf = (CTRL2_DFC_MASK & CTRL2_DFC_9) | ((~CTRL2_DFC_MASK) & buf);
-       k2hh_i2c_write(data, CTRL2_REG, buf);
-       SENSOR_INFO("ctrl2:%x\n", buf);
-#endif
-
        return ret;
 }
 
@@ -394,7 +388,6 @@ static int k2hh_set_bw(struct k2hh_p *data)
 
        return ret;
 }
-
 static int k2hh_set_hr(struct k2hh_p *data, int set)
 {
        int ret;
@@ -417,8 +410,8 @@ static int k2hh_set_hr(struct k2hh_p *data, int set)
 #if defined(OUTPUT_ALWAYS_ANTI_ALIASED)
                bw = K2HH_ACC_BW_SCALE_ODR_DISABLE;
                k2hh_i2c_read(data, CTRL4_REG, &buf, 1);
-               buf = (K2HH_ACC_BW_SCALE_ODR_MASK & bw) |
-                       ((~K2HH_ACC_BW_SCALE_ODR_MASK) & buf);
+               buf = (K2HH_ACC_BW_SCALE_ODR_MASK & bw)
+                               | ((~K2HH_ACC_BW_SCALE_ODR_MASK) & buf);
                k2hh_i2c_write(data, CTRL4_REG, buf);
 #endif
        }
@@ -430,6 +423,7 @@ static int k2hh_set_hr(struct k2hh_p *data, int set)
        buf = ((K2HH_ACC_ODR_MASK & odr) | ((~K2HH_ACC_ODR_MASK) & buf));
        ret += k2hh_i2c_write(data, CTRL1_REG, buf);
 
+
        return ret;
 }
 
@@ -440,7 +434,7 @@ static void k2hh_set_enable(struct k2hh_p *data, int enable)
                      HRTIMER_MODE_REL);
        } else {
                hrtimer_cancel(&data->accel_timer);
-               cancel_work_sync(&data->work_accel);
+               cancel_work_sync(&data->work);
        }
 }
 
@@ -496,7 +490,7 @@ static int k2hh_open_calibration(struct k2hh_p *data)
                data->caldata.y = 0;
                data->caldata.z = 0;
 
-               SENSOR_INFO("No Calibration\n");
+               SENSOR_ERR(" No Calibration\n");
 
                return ret;
        }
@@ -504,7 +498,7 @@ static int k2hh_open_calibration(struct k2hh_p *data)
        ret = cal_filp->f_op->read(cal_filp, (char *)&data->caldata.v,
                3 * sizeof(s16), &cal_filp->f_pos);
        if (ret != 3 * sizeof(s16)) {
-               SENSOR_ERR("can't read the cal data\n");
+               SENSOR_ERR("Can't read the cal data\n");
                ret = -EIO;
        }
 
@@ -529,11 +523,11 @@ static int k2hh_do_calibrate(struct k2hh_p *data, int enable)
        struct k2hh_v acc;
        mm_segment_t old_fs;
 
-       data->caldata.x = 0;
-       data->caldata.y = 0;
-       data->caldata.z = 0;
-
        if (enable) {
+               data->caldata.x = 0;
+               data->caldata.y = 0;
+               data->caldata.z = 0;
+
                if (atomic_read(&data->enable) == ON)
                        k2hh_set_enable(data, OFF);
                else
@@ -562,6 +556,10 @@ static int k2hh_do_calibrate(struct k2hh_p *data, int enable)
                        data->caldata.z -= MAX_ACCEL_1G;
                else if (data->caldata.z < 0)
                        data->caldata.z += MAX_ACCEL_1G;
+       } else {
+               data->caldata.x = 0;
+               data->caldata.y = 0;
+               data->caldata.z = 0;
        }
 
        SENSOR_INFO("do accel calibrate %d, %d, %d\n",
@@ -573,7 +571,7 @@ static int k2hh_do_calibrate(struct k2hh_p *data, int enable)
        cal_filp = filp_open(CALIBRATION_FILE_PATH,
                        O_CREAT | O_TRUNC | O_WRONLY, 0660);
        if (IS_ERR(cal_filp)) {
-               SENSOR_ERR("can't open calibration file\n");
+               SENSOR_ERR(" Can't open calibration file\n");
                set_fs(old_fs);
                ret = PTR_ERR(cal_filp);
                return ret;
@@ -582,7 +580,7 @@ static int k2hh_do_calibrate(struct k2hh_p *data, int enable)
        ret = cal_filp->f_op->write(cal_filp, (char *)&data->caldata.v,
                3 * sizeof(s16), &cal_filp->f_pos);
        if (ret != 3 * sizeof(s16)) {
-               SENSOR_ERR("can't write the caldata to file\n");
+               SENSOR_ERR(" Can't write the caldata to file\n");
                ret = -EIO;
        }
 
@@ -597,8 +595,8 @@ static enum hrtimer_restart k2hh_timer_func(struct hrtimer *timer)
        struct k2hh_p *data = container_of(timer,
                                        struct k2hh_p, accel_timer);
 
-       if (!work_pending(&data->work_accel))
-               queue_work(data->accel_wq, &data->work_accel);
+       if (!work_pending(&data->work))
+               queue_work(data->accel_wq, &data->work);
 
        hrtimer_forward_now(&data->accel_timer, data->poll_delay);
 
@@ -607,35 +605,18 @@ static enum hrtimer_restart k2hh_timer_func(struct hrtimer *timer)
 
 static void k2hh_work_func(struct work_struct *work)
 {
+       int ret;
        struct k2hh_v acc;
-       struct k2hh_p *data = container_of(work, struct k2hh_p, work_accel);
+       struct k2hh_p *data = container_of(work, struct k2hh_p, work);
        struct timespec ts;
        u64 timestamp_new;
        u64 delay = ktime_to_ns(data->poll_delay);
        int time_hi, time_lo;
-       int ret;
 
        ret = k2hh_read_accel_xyz(data, &acc);
        if (ret < 0)
                goto exit;
 
-#if defined(ENABLE_LOG_ACCEL_MAX_OUT)
-       /* For debugging if happened exceptional situation */
-       if (acc.x > ACCEL_MAX_OUTPUT ||
-                       acc.y > ACCEL_MAX_OUTPUT ||
-                       acc.z > ACCEL_MAX_OUTPUT) {
-               unsigned char buf[4], status;
-
-               k2hh_i2c_read(data, CTRL1_REG, buf, 4);
-               k2hh_i2c_read(data, STATUS_REG, &status, 1);
-
-               SENSOR_INFO("MAX_OUTPUT x = %d, y = %d, z = %d\n",
-                               acc.x, acc.y, acc.z);
-               SENSOR_INFO("CTRL(20h~23h) : %X, %X, %X, %X - STATUS(27h) : %X\n",
-                               buf[0], buf[1], buf[2], buf[3], status);
-       }
-#endif
-
        ts = ktime_to_timespec(ktime_get_boottime());
        timestamp_new = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
 
@@ -643,12 +624,13 @@ static void k2hh_work_func(struct work_struct *work)
        data->accdata.y = acc.y - data->caldata.y;
        data->accdata.z = acc.z - data->caldata.z;
 
-       if (((timestamp_new - data->old_timestamp) * 10 > delay * 18)
-               && (data->old_timestamp != 0)) {
+       if (((timestamp_new - data->old_timestamp)*10 > delay *18)\
+               && (data->old_timestamp != 0))
+       {
                u64 shift_timestamp = delay >> 1;
                u64 timestamp = 0ULL;
 
-               for (timestamp = data->old_timestamp + delay; timestamp < timestamp_new - shift_timestamp; timestamp += delay) {
+               for (timestamp = data->old_timestamp + delay; timestamp < timestamp_new - shift_timestamp; timestamp+=delay){
                        time_hi = (int)((timestamp & TIME_HI_MASK) >> TIME_HI_SHIFT);
                        time_lo = (int)(timestamp & TIME_LO_MASK);
                        input_report_rel(data->input, REL_X, data->accdata.x);
@@ -661,7 +643,6 @@ static void k2hh_work_func(struct work_struct *work)
        }
        time_hi = (int)((timestamp_new & TIME_HI_MASK) >> TIME_HI_SHIFT);
        time_lo = (int)(timestamp_new & TIME_LO_MASK);
-
        input_report_rel(data->input, REL_X, data->accdata.x);
        input_report_rel(data->input, REL_Y, data->accdata.y);
        input_report_rel(data->input, REL_Z, data->accdata.z);
@@ -696,25 +677,19 @@ static ssize_t k2hh_enable_store(struct device *dev,
        u8 enable;
        int ret, pre_enable;
        struct k2hh_p *data = dev_get_drvdata(dev);
-
+       data->old_timestamp = 0LL;
        ret = kstrtou8(buf, 2, &enable);
        if (ret) {
-               SENSOR_ERR("Invalid Argument\n");
+               SENSOR_ERR(" Invalid Argument\n");
                return ret;
        }
 
+       SENSOR_INFO("new_value = %u\n",enable);
        pre_enable = atomic_read(&data->enable);
-       SENSOR_INFO("pre_enable = %d, enable = %d\n", pre_enable, enable);
 
        if (enable) {
                if (pre_enable == OFF) {
-                       data->old_timestamp = 0LL;
-
-                       /* Access to calibration data in filesystem
-                        * only when there are no caldata */
-                       if (data->caldata.x == 0 && data->caldata.y == 0
-                                       && data->caldata.z == 0)
-                               k2hh_open_calibration(data);
+                       k2hh_open_calibration(data);
                        k2hh_set_range(data, K2HH_RANGE_4G);
                        k2hh_set_bw(data);
                        k2hh_set_mode(data, K2HH_MODE_NORMAL);
@@ -747,17 +722,16 @@ static ssize_t k2hh_delay_store(struct device *dev,
        int ret;
        int64_t delay;
        struct k2hh_p *data = dev_get_drvdata(dev);
-
+       data->old_timestamp = 0LL;
        ret = kstrtoll(buf, 10, &delay);
        if (ret) {
-               SENSOR_ERR("Invalid Argument\n");
+               SENSOR_ERR(" Invalid Argument\n");
                return ret;
        }
-
        if (delay > K2HH_DEFAULT_DELAY)
                delay = K2HH_DEFAULT_DELAY;
-       else if (delay < K2HH_MIN_DELAY)
-               delay = K2HH_MIN_DELAY;
+       else if(delay < K2HH_MIN_DELAY)
+               delay = K2HH_MIN_DELAY ;
 
        data->poll_delay = ns_to_ktime(delay);
        k2hh_set_odr(data);
@@ -786,6 +760,7 @@ static struct attribute_group k2hh_attribute_group = {
        .attrs = k2hh_attributes
 };
 
+
 static ssize_t k2hh_vendor_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
@@ -806,29 +781,29 @@ static ssize_t k2hh_calibration_show(struct device *dev,
 
        ret = k2hh_open_calibration(data);
        if (ret < 0)
-               SENSOR_ERR("calibration open failed(%d)\n", ret);
+               SENSOR_ERR(" calibration open failed(%d)\n", ret);
 
-       SENSOR_INFO("cal data %d %d %d - ret : %d\n",
+       SENSOR_INFO("cal data %d %d %d - ret : %d\n", 
                data->caldata.x, data->caldata.y, data->caldata.z, ret);
 
-       return snprintf(buf, PAGE_SIZE, "%d %d %d %d\n", ret,
-               data->caldata.x, data->caldata.y, data->caldata.z);
+       return snprintf(buf, PAGE_SIZE, "%d %d %d %d\n", ret, data->caldata.x,
+                       data->caldata.y, data->caldata.z);
 }
 
 static ssize_t k2hh_calibration_store(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t size)
 {
        int ret;
-       int64_t d_enable;
+       int64_t dEnable;
        struct k2hh_p *data = dev_get_drvdata(dev);
 
-       ret = kstrtoll(buf, 10, &d_enable);
+       ret = kstrtoll(buf, 10, &dEnable);
        if (ret < 0)
                return ret;
 
-       ret = k2hh_do_calibrate(data, (int)d_enable);
+       ret = k2hh_do_calibrate(data, (int)dEnable);
        if (ret < 0)
-               SENSOR_ERR("accel calibrate failed\n");
+               SENSOR_ERR(" accel calibrate failed\n");
 
        return size;
 }
@@ -851,18 +826,18 @@ static ssize_t k2hh_lowpassfilter_store(struct device *dev,
        struct device_attribute *attr, const char *buf, size_t size)
 {
        int ret;
-       int64_t d_enable;
+       int64_t dEnable;
        struct k2hh_p *data = dev_get_drvdata(dev);
 
        SENSOR_INFO("\n");
 
-       ret = kstrtoll(buf, 10, &d_enable);
+       ret = kstrtoll(buf, 10, &dEnable);
        if (ret < 0)
                SENSOR_ERR("kstrtoll failed\n");
 
-       ret = k2hh_set_hr(data, d_enable);
+       ret = k2hh_set_hr(data, dEnable);
        if (ret < 0)
-               SENSOR_ERR("set_hr failed\n");
+               SENSOR_ERR("k303c_acc_set_hr failed\n");
 
        return size;
 }
@@ -874,6 +849,7 @@ static ssize_t k2hh_raw_data_read(struct device *dev,
        struct k2hh_p *data = dev_get_drvdata(dev);
 
        if (atomic_read(&data->enable) == OFF) {
+               SENSOR_INFO(", atomic_read\n");
                k2hh_set_mode(data, K2HH_MODE_NORMAL);
                msleep(20);
                k2hh_read_accel_xyz(data, &acc);
@@ -882,8 +858,9 @@ static ssize_t k2hh_raw_data_read(struct device *dev,
                acc.x = acc.x - data->caldata.x;
                acc.y = acc.y - data->caldata.y;
                acc.z = acc.z - data->caldata.z;
-       } else
+       } else {
                acc = data->accdata;
+       }
 
        return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
                        acc.x, acc.y, acc.z);
@@ -918,7 +895,7 @@ static ssize_t k2hh_reactive_alert_store(struct device *dev,
                factory_mode = ON;
                SENSOR_INFO("factory mode\n");
        } else {
-               SENSOR_ERR("invalid value %d\n", *buf);
+               SENSOR_ERR(" invalid value %d\n", *buf);
                return -EINVAL;
        }
 
@@ -983,7 +960,7 @@ static ssize_t k2hh_selftest_show(struct device *dev,
        struct k2hh_p *data = dev_get_drvdata(dev);
        struct k2hh_v acc;
        unsigned char temp, backup[4];
-       int result = 1, i, retry;
+       int result = 1, i;
        ssize_t ret;
        s32 NO_ST[3] = {0, 0, 0};
        s32 ST[3] = {0, 0, 0};
@@ -1008,32 +985,21 @@ static ssize_t k2hh_selftest_show(struct device *dev,
        k2hh_read_accel_xyz(data, &acc);
 
        for (i = 0; i < 5; i++) {
-               retry = 5;
-               do {
-                       ret = k2hh_i2c_read(data, STATUS_REG, &temp, 1);
-                       if (ret < 0) {
+               while (1) {
+                       if (k2hh_i2c_read(data, STATUS_REG, &temp, 1) < 0) {
                                SENSOR_ERR("i2c error");
                                goto exit_status_err;
                        }
 
                        if (temp & 0x08)
                                break;
-
-                       msleep(20);
-               } while (retry-- >= 0);
-
-               if (retry < 0) {
-                       SENSOR_ERR("failed to update data\n");
-                       goto exit_status_err;
                }
 
                k2hh_read_accel_xyz(data, &acc);
-
                NO_ST[0] += acc.x;
                NO_ST[1] += acc.y;
                NO_ST[2] += acc.z;
        }
-
        NO_ST[0] /= 5;
        NO_ST[1] /= 5;
        NO_ST[2] /= 5;
@@ -1045,23 +1011,14 @@ static ssize_t k2hh_selftest_show(struct device *dev,
        k2hh_read_accel_xyz(data, &acc);
 
        for (i = 0; i < 5; i++) {
-               retry = 5;
-               do {
-                       ret = k2hh_i2c_read(data, STATUS_REG, &temp, 1);
-                       if (ret < 0) {
+               while (1) {
+                       if (k2hh_i2c_read(data, STATUS_REG, &temp, 1) < 0) {
                                SENSOR_ERR("i2c error");
                                goto exit_status_err;
                        }
 
                        if (temp & 0x08)
                                break;
-
-                       msleep(20);
-               } while (retry-- >= 0);
-
-               if (retry < 0) {
-                       SENSOR_ERR("failed to update data\n");
-                       goto exit_status_err;
                }
 
                k2hh_read_accel_xyz(data, &acc);
@@ -1080,7 +1037,7 @@ static ssize_t k2hh_selftest_show(struct device *dev,
 
                if ((SELF_TEST_2G_MIN_LSB > ST[i])
                        || (ST[i] > SELF_TEST_2G_MAX_LSB)) {
-                       SENSOR_ERR("%d Out of range!! (%d)\n", i, ST[i]);
+                       SENSOR_ERR("%d Out of range!! (%d)\n",i, ST[i]);
                        result = 0;
                }
        }
@@ -1171,7 +1128,7 @@ static int k2hh_setup_pin(struct k2hh_p *data)
 
        ret = gpio_direction_input(data->acc_int1);
        if (ret < 0) {
-               SENSOR_ERR("failed to set gpio %d as input (%d)\n",
+               SENSOR_ERR(" failed to set gpio %d as input (%d)\n",
                        data->acc_int1, ret);
                goto exit_acc_int1;
        }
@@ -1180,11 +1137,10 @@ static int k2hh_setup_pin(struct k2hh_p *data)
                "reactive_wake_lock");
 
        data->irq1 = gpio_to_irq(data->acc_int1);
-       /* add IRQF_NO_SUSPEND option in case of Spreadtrum AP */
        ret = request_threaded_irq(data->irq1, NULL, k2hh_irq_thread,
                IRQF_TRIGGER_RISING | IRQF_ONESHOT, "k2hh_accel", data);
        if (ret < 0) {
-               SENSOR_ERR("can't allocate irq.\n");
+               SENSOR_ERR(" can't allocate irq.\n");
                goto exit_reactive_irq;
        }
 
@@ -1246,21 +1202,22 @@ err_register_input_dev:
 
 static int k2hh_parse_dt(struct k2hh_p *data, struct device *dev)
 {
-       struct device_node *d_node = dev->of_node;
+       struct device_node *dNode = dev->of_node;
        enum of_gpio_flags flags;
        int ret;
        u32 temp;
 
-       if (d_node == NULL)
+       if (dNode == NULL)
                return -ENODEV;
 
-       data->acc_int1 = of_get_named_gpio_flags(d_node, "k2hh,irq_gpio", 0, &flags);
+       data->acc_int1 = of_get_named_gpio_flags(dNode, "k2hh,irq_gpio", 0,
+               &flags);
        if (data->acc_int1 < 0) {
                SENSOR_ERR("get acc_int1 error\n");
                return -ENODEV;
        }
 
-       ret = of_property_read_u32(d_node, "k2hh,axis_map_x", &temp);
+       ret = of_property_read_u32(dNode, "k2hh,axis_map_x", &temp);
        if ((data->axis_map_x > 2) || (ret < 0)) {
                SENSOR_ERR("invalid x axis_map value %u\n",
                        data->axis_map_x);
@@ -1268,7 +1225,7 @@ static int k2hh_parse_dt(struct k2hh_p *data, struct device *dev)
        } else
                data->axis_map_x = (u8)temp;
 
-       ret = of_property_read_u32(d_node, "k2hh,axis_map_y", &temp);
+       ret = of_property_read_u32(dNode, "k2hh,axis_map_y", &temp);
        if ((data->axis_map_y > 2) || (ret < 0)) {
                SENSOR_ERR("invalid y axis_map value %u\n",
                        data->axis_map_y);
@@ -1276,7 +1233,7 @@ static int k2hh_parse_dt(struct k2hh_p *data, struct device *dev)
        } else
                data->axis_map_y = (u8)temp;
 
-       ret = of_property_read_u32(d_node, "k2hh,axis_map_z", &temp);
+       ret = of_property_read_u32(dNode, "k2hh,axis_map_z", &temp);
        if ((data->axis_map_z > 2) || (ret < 0)) {
                SENSOR_ERR("invalid z axis_map value %u\n",
                        data->axis_map_z);
@@ -1284,7 +1241,7 @@ static int k2hh_parse_dt(struct k2hh_p *data, struct device *dev)
        } else
                data->axis_map_z = (u8)temp;
 
-       ret = of_property_read_u32(d_node, "k2hh,negate_x", &temp);
+       ret = of_property_read_u32(dNode, "k2hh,negate_x", &temp);
        if ((data->negate_x > 1) || (ret < 0)) {
                SENSOR_ERR("invalid x axis_map value %u\n",
                        data->negate_x);
@@ -1292,7 +1249,7 @@ static int k2hh_parse_dt(struct k2hh_p *data, struct device *dev)
        } else
                data->negate_x = (u8)temp;
 
-       ret = of_property_read_u32(d_node, "k2hh,negate_y", &temp);
+       ret = of_property_read_u32(dNode, "k2hh,negate_y", &temp);
        if ((data->negate_y > 1) || (ret < 0)) {
                SENSOR_ERR("invalid y axis_map value %u\n",
                        data->negate_y);
@@ -1300,7 +1257,7 @@ static int k2hh_parse_dt(struct k2hh_p *data, struct device *dev)
        } else
                data->negate_y = (u8)temp;
 
-       ret = of_property_read_u32(d_node, "k2hh,negate_z", &temp);
+       ret = of_property_read_u32(dNode, "k2hh,negate_z", &temp);
        if ((data->negate_z > 1) || (ret < 0)) {
                SENSOR_ERR("invalid z axis_map value %u\n",
                        data->negate_z);
@@ -1315,11 +1272,11 @@ static int k2hh_regulator_onoff(struct k2hh_p *data, bool onoff)
 {
        int ret = 0;
 
-       SENSOR_INFO("%s\n", (onoff) ? "on" : "off");
+       SENSOR_INFO(" %s\n", (onoff) ? "on" : "off");
 
 #ifdef CONFIG_SENSORS_K2HH_VDD
        if (!data->reg_vdd) {
-               SENSOR_INFO("VDD get regulator\n");
+               SENSOR_INFO(" VDD get regulator\n");
                data->reg_vdd = devm_regulator_get(&data->client->dev,
                        "k2hh,vdd");
                if (IS_ERR(data->reg_vdd)) {
@@ -1349,7 +1306,7 @@ static int k2hh_regulator_onoff(struct k2hh_p *data, bool onoff)
 #ifdef CONFIG_SENSORS_K2HH_VDD
                ret = regulator_enable(data->reg_vdd);
                if (ret)
-                       SENSOR_ERR("failed to enable vdd.\n");
+                       SENSOR_ERR("Failed to enable vdd.\n");
 #endif
                if(!regulator_is_enabled(data->reg_vio))
                {
@@ -1364,11 +1321,11 @@ static int k2hh_regulator_onoff(struct k2hh_p *data, bool onoff)
 #ifdef CONFIG_SENSORS_K2HH_VDD
                ret = regulator_disable(data->reg_vdd);
                if (ret)
-                       SENSOR_ERR("failed to disable vdd.\n");
+                       SENSOR_ERR("Failed to disable vdd.\n");
 #endif
                ret = regulator_disable(data->reg_vio);
                if (ret)
-                       SENSOR_ERR("failed to disable vio.\n");
+                       SENSOR_ERR("Failed to disable vio.\n");
                msleep(30);
        }
 
@@ -1376,7 +1333,7 @@ static int k2hh_regulator_onoff(struct k2hh_p *data, bool onoff)
 
 err_vio:
 #ifdef CONFIG_SENSORS_K2HH_VDD
-       SENSOR_INFO("VDD put\n");
+       SENSOR_INFO(" VDD put\n");
        devm_regulator_put(data->reg_vdd);
 err_vdd:
 #endif
@@ -1389,20 +1346,20 @@ static int k2hh_probe(struct i2c_client *client,
 {
        u8 temp;
        int ret = -ENODEV, i;
-       struct k2hh_p *data;
+       struct k2hh_p *data = NULL;
 
-       SENSOR_INFO("start!\n");
+       SENSOR_INFO("2222 Probe Start!\n");
 
        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
-               SENSOR_ERR("i2c_check_functionality error\n");
+               SENSOR_ERR(" i2c_check_functionality error\n");
                goto exit;
        }
 
        data = kzalloc(sizeof(struct k2hh_p), GFP_KERNEL);
        if (data == NULL) {
-               SENSOR_ERR("kzalloc error\n");
+               SENSOR_ERR(" kzalloc error\n");
                ret = -ENOMEM;
-               goto exit;
+               goto exit_kzalloc;
        }
 
        i2c_set_clientdata(client, data);
@@ -1410,15 +1367,21 @@ static int k2hh_probe(struct i2c_client *client,
 
        ret = k2hh_parse_dt(data, &client->dev);
        if (ret < 0) {
-               SENSOR_ERR("of_node error\n");
+               SENSOR_ERR(" of_node error\n");
                ret = -ENODEV;
                goto exit_of_node;
        }
 
        ret = k2hh_regulator_onoff(data, true);
        if (ret < 0) {
-               SENSOR_ERR("No regulator\n");
-               goto exit_no_regulator;
+               SENSOR_ERR(" No regulator\n");
+               goto exit_of_node;
+       }
+
+       ret = k2hh_setup_pin(data);
+       if (ret < 0) {
+               SENSOR_ERR(" could not setup pin\n");
+               goto exit_setup_pin;
        }
 
        mutex_init(&data->mode_mutex);
@@ -1448,12 +1411,8 @@ static int k2hh_probe(struct i2c_client *client,
        if (ret < 0)
                goto exit_input_init;
 
-       ret = sensors_register(data->factory_device, data, sensor_attrs,
+       sensors_register(data->factory_device, data, sensor_attrs,
                MODULE_NAME);
-       if (ret) {
-               SENSOR_ERR("failed to sensors_register (%d)\n", ret);
-               goto exit_sensor_register_failed;
-       }
 
        /* accel_timer settings. we poll for light values using a timer. */
        hrtimer_init(&data->accel_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
@@ -1465,20 +1424,14 @@ static int k2hh_probe(struct i2c_client *client,
        data->accel_wq = create_singlethread_workqueue("accel_wq");
        if (!data->accel_wq) {
                ret = -ENOMEM;
-               SENSOR_ERR("could not create workqueue\n");
+               SENSOR_ERR(" could not create workqueue\n");
                goto exit_create_workqueue;
        }
 
        /* this is the thread function we run on the work queue */
-       INIT_WORK(&data->work_accel, k2hh_work_func);
+       INIT_WORK(&data->work, k2hh_work_func);
        INIT_DELAYED_WORK(&data->irq_work, k2hh_irq_work_func);
 
-       ret = k2hh_setup_pin(data);
-       if (ret < 0) {
-               SENSOR_ERR("could not setup pin\n");
-               goto exit_setup_pin;
-       }
-
        atomic_set(&data->enable, OFF);
        data->time_count = 0;
        data->irq_state = 0;
@@ -1488,28 +1441,28 @@ static int k2hh_probe(struct i2c_client *client,
        k2hh_set_range(data, K2HH_RANGE_4G);
        k2hh_set_mode(data, K2HH_MODE_SUSPEND);
 
-       SENSOR_INFO("done!\n");
+       SENSOR_INFO("Probe done!\n");
 
        return 0;
 
-exit_setup_pin:
-       cancel_delayed_work_sync(&data->irq_work);
-       destroy_workqueue(data->accel_wq);
 exit_create_workqueue:
        sensors_unregister(data->factory_device, sensor_attrs);
-exit_sensor_register_failed:
        sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
        sysfs_remove_group(&data->input->dev.kobj, &k2hh_attribute_group);
        input_unregister_device(data->input);
 exit_input_init:
 exit_read_chipid:
        mutex_destroy(&data->mode_mutex);
+       free_irq(data->irq1, data);
+       wake_lock_destroy(&data->reactive_wake_lock);
+       gpio_free(data->acc_int1);
+exit_setup_pin:
        k2hh_regulator_onoff(data, false);
-exit_no_regulator:
 exit_of_node:
        kfree(data);
+exit_kzalloc:
 exit:
-       SENSOR_ERR("fail!\n");
+       SENSOR_ERR(" Probe fail!\n");
        return ret;
 }
 
@@ -1589,13 +1542,14 @@ static void k2hh_shutdown(struct i2c_client *client)
        k2hh_set_mode(data, K2HH_MODE_SUSPEND);
 }
 
+
 static struct of_device_id k2hh_match_table[] = {
        { .compatible = "k2hh-i2c",},
        {},
 };
 
 static const struct i2c_device_id k2hh_id[] = {
-       { "k2hh_match_table", 0 },
+       { "k2hh-i2c", 0 },
        { }
 };
 
@@ -1619,11 +1573,11 @@ static struct i2c_driver k2hh_driver = {
 
 static int __init k2hh_init(void)
 {
-       if(lpcharge) {
-               SENSOR_INFO("Skip init due to low power charging mode %d", lpcharge);
+#ifdef CONFIG_SENSORS_LDO_CONTROL
+       if(lpcharge)
                return 0;
-       }
        else
+#endif
                return i2c_add_driver(&k2hh_driver);
 }