123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- /*
- * Copyright (C) 2012, Samsung Electronics Co. Ltd. All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- */
- #include "../ssp.h"
- /*************************************************************************/
- /* factory Sysfs */
- /*************************************************************************/
- #define VENDOR "INVENSENSE"
- #define CHIP_ID "MPU6500"
- #ifdef CONFIG_MACH_KACTIVELTE_KOR
- #define CALIBRATION_FILE_PATH "/efs/FactoryApp/calibration_data"
- #else
- #define CALIBRATION_FILE_PATH "/efs/calibration_data"
- #endif
- #define CALIBRATION_DATA_AMOUNT 20
- static ssize_t accel_vendor_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- return sprintf(buf, "%s\n", VENDOR);
- }
- static ssize_t accel_name_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- return sprintf(buf, "%s\n", CHIP_ID);
- }
- int accel_open_calibration(struct ssp_data *data)
- {
- int iRet = 0;
- mm_segment_t old_fs;
- struct file *cal_filp = NULL;
- old_fs = get_fs();
- set_fs(KERNEL_DS);
- cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY | O_NOFOLLOW | O_NONBLOCK, 0660);
- if (IS_ERR(cal_filp)) {
- set_fs(old_fs);
- iRet = PTR_ERR(cal_filp);
- data->accelcal.x = 0;
- data->accelcal.y = 0;
- data->accelcal.z = 0;
- return iRet;
- }
- iRet = cal_filp->f_op->read(cal_filp, (char *)&data->accelcal,
- 3 * sizeof(int), &cal_filp->f_pos);
- if (iRet != 3 * sizeof(int))
- iRet = -EIO;
- filp_close(cal_filp, current->files);
- set_fs(old_fs);
- ssp_dbg("[SSP]: open accel calibration %d, %d, %d\n",
- data->accelcal.x, data->accelcal.y, data->accelcal.z);
- if ((data->accelcal.x == 0) && (data->accelcal.y == 0)
- && (data->accelcal.z == 0))
- return ERROR;
- return iRet;
- }
- int set_accel_cal(struct ssp_data *data)
- {
- int iRet = 0;
- struct ssp_msg *msg;
- s16 accel_cal[3];
- if (!(data->uSensorState & (1 << ACCELEROMETER_SENSOR))) {
- pr_info("[SSP]: %s - Skip this function!!!"\
- ", accel sensor is not connected(0x%x)\n",
- __func__, data->uSensorState);
- return iRet;
- }
- accel_cal[0] = data->accelcal.x;
- accel_cal[1] = data->accelcal.y;
- accel_cal[2] = data->accelcal.z;
- msg = kzalloc(sizeof(*msg), GFP_KERNEL);
- msg->cmd = MSG2SSP_AP_MCU_SET_ACCEL_CAL;
- msg->length = 6;
- msg->options = AP2HUB_WRITE;
- msg->buffer = (char*) kzalloc(6, GFP_KERNEL);
- msg->free_buffer = 1;
- memcpy(msg->buffer, accel_cal, 6);
- iRet = ssp_spi_async(data, msg);
- if (iRet != SUCCESS) {
- pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
- iRet = ERROR;
- }
- pr_info("[SSP] Set accel cal data %d, %d, %d\n", accel_cal[0], accel_cal[1], accel_cal[2]);
- return iRet;
- }
- static int enable_accel_for_cal(struct ssp_data *data)
- {
- u8 uBuf[9] = { 0, };
- s32 dMsDelay = get_msdelay(data->adDelayBuf[ACCELEROMETER_SENSOR]);
- memcpy(&uBuf[0], &dMsDelay, 4);
- if (atomic_read(&data->aSensorEnable) & (1 << ACCELEROMETER_SENSOR)) {
- if (get_msdelay(data->adDelayBuf[ACCELEROMETER_SENSOR]) != 10) {
- send_instruction(data, CHANGE_DELAY,
- ACCELEROMETER_SENSOR, uBuf, 9);
- return SUCCESS;
- }
- } else {
- send_instruction(data, ADD_SENSOR,
- ACCELEROMETER_SENSOR, uBuf, 9);
- }
- return FAIL;
- }
- static void disable_accel_for_cal(struct ssp_data *data, int iDelayChanged)
- {
- u8 uBuf[9] = { 0, };
- s32 dMsDelay = get_msdelay(data->adDelayBuf[ACCELEROMETER_SENSOR]);
- memcpy(&uBuf[0], &dMsDelay, 4);
- if (atomic_read(&data->aSensorEnable) & (1 << ACCELEROMETER_SENSOR)) {
- if (iDelayChanged)
- send_instruction(data, CHANGE_DELAY,
- ACCELEROMETER_SENSOR, uBuf, 9);
- } else {
- send_instruction(data, REMOVE_SENSOR,
- ACCELEROMETER_SENSOR, uBuf, 4);
- }
- }
- static int accel_do_calibrate(struct ssp_data *data, int iEnable)
- {
- int iSum[3] = { 0, };
- int iRet = 0, iCount;
- struct file *cal_filp = NULL;
- mm_segment_t old_fs;
- if (iEnable) {
- data->accelcal.x = 0;
- data->accelcal.y = 0;
- data->accelcal.z = 0;
- set_accel_cal(data);
- iRet = enable_accel_for_cal(data);
- msleep(300);
- for (iCount = 0; iCount < CALIBRATION_DATA_AMOUNT; iCount++) {
- iSum[0] += data->buf[ACCELEROMETER_SENSOR].x;
- iSum[1] += data->buf[ACCELEROMETER_SENSOR].y;
- iSum[2] += data->buf[ACCELEROMETER_SENSOR].z;
- mdelay(10);
- }
- disable_accel_for_cal(data, iRet);
- data->accelcal.x = (iSum[0] / CALIBRATION_DATA_AMOUNT);
- data->accelcal.y = (iSum[1] / CALIBRATION_DATA_AMOUNT);
- data->accelcal.z = (iSum[2] / CALIBRATION_DATA_AMOUNT);
- if (data->accelcal.z > 0)
- data->accelcal.z -= MAX_ACCEL_1G;
- else if (data->accelcal.z < 0)
- data->accelcal.z += MAX_ACCEL_1G;
- } else {
- data->accelcal.x = 0;
- data->accelcal.y = 0;
- data->accelcal.z = 0;
- }
- ssp_dbg("[SSP]: do accel calibrate %d, %d, %d\n",
- data->accelcal.x, data->accelcal.y, data->accelcal.z);
- old_fs = get_fs();
- set_fs(KERNEL_DS);
- cal_filp = filp_open(CALIBRATION_FILE_PATH,
- O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_NONBLOCK, 0660);
- if (IS_ERR(cal_filp)) {
- pr_err("[SSP]: %s - Can't open calibration file\n", __func__);
- set_fs(old_fs);
- iRet = PTR_ERR(cal_filp);
- return iRet;
- }
- iRet = cal_filp->f_op->write(cal_filp, (char *)&data->accelcal,
- 3 * sizeof(int), &cal_filp->f_pos);
- if (iRet != 3 * sizeof(int)) {
- pr_err("[SSP]: %s - Can't write the accelcal to file\n",
- __func__);
- iRet = -EIO;
- }
- filp_close(cal_filp, current->files);
- set_fs(old_fs);
- set_accel_cal(data);
- return iRet;
- }
- static ssize_t accel_calibration_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- int iRet;
- int iCount = 0;
- struct ssp_data *data = dev_get_drvdata(dev);
- iRet = accel_open_calibration(data);
- if (iRet < 0)
- pr_err("[SSP]: %s - calibration open failed(%d)\n", __func__, iRet);
- ssp_dbg("[SSP] Cal data : %d %d %d - %d\n",
- data->accelcal.x, data->accelcal.y, data->accelcal.z, iRet);
- iCount = sprintf(buf, "%d %d %d %d\n", iRet, data->accelcal.x,
- data->accelcal.y, data->accelcal.z);
- return iCount;
- }
- static ssize_t accel_calibration_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t size)
- {
- int iRet;
- int64_t dEnable;
- struct ssp_data *data = dev_get_drvdata(dev);
- iRet = kstrtoll(buf, 10, &dEnable);
- if (iRet < 0)
- return iRet;
- iRet = accel_do_calibrate(data, (int)dEnable);
- if (iRet < 0)
- pr_err("[SSP]: %s - accel_do_calibrate() failed\n", __func__);
- return size;
- }
- static ssize_t raw_data_read(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct ssp_data *data = dev_get_drvdata(dev);
- return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
- data->buf[ACCELEROMETER_SENSOR].x,
- data->buf[ACCELEROMETER_SENSOR].y,
- data->buf[ACCELEROMETER_SENSOR].z);
- }
- static ssize_t accel_reactive_alert_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t size)
- {
- int iRet = 0;
- char chTempBuf = 1;
- struct ssp_data *data = dev_get_drvdata(dev);
- struct ssp_msg *msg;
- if (sysfs_streq(buf, "1"))
- ssp_dbg("[SSP]: %s - on\n", __func__);
- else if (sysfs_streq(buf, "0"))
- ssp_dbg("[SSP]: %s - off\n", __func__);
- else if (sysfs_streq(buf, "2")) {
- ssp_dbg("[SSP]: %s - factory\n", __func__);
- data->bAccelAlert = 0;
- msg = kzalloc(sizeof(*msg), GFP_KERNEL);
- msg->cmd = ACCELEROMETER_FACTORY;
- msg->length = 1;
- msg->options = AP2HUB_READ;
- msg->data = chTempBuf;
- msg->buffer = &chTempBuf;
- msg->free_buffer = 0;
- iRet = ssp_spi_sync(data, msg, 3000);
- data->bAccelAlert = chTempBuf;
- if (iRet != SUCCESS) {
- pr_err("[SSP]: %s - accel Selftest Timeout!!\n", __func__);
- goto exit;
- }
- ssp_dbg("[SSP]: %s factory test success!\n", __func__);
- } else {
- pr_err("[SSP]: %s - invalid value %d\n", __func__, *buf);
- return -EINVAL;
- }
- exit: return size;
- }
- static ssize_t accel_reactive_alert_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- bool bSuccess = false;
- struct ssp_data *data = dev_get_drvdata(dev);
- if (data->bAccelAlert == true)
- bSuccess = true;
- else
- bSuccess = false;
- data->bAccelAlert = false;
- return sprintf(buf, "%u\n", bSuccess);
- }
- static ssize_t accel_hw_selftest_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- char chTempBuf[8] = { 2, 0, };
- s8 init_status = 0, result = -1;
- s16 shift_ratio[3] = { 0, };
- int iRet;
- struct ssp_data *data = dev_get_drvdata(dev);
- struct ssp_msg *msg;
- msg = kzalloc(sizeof(*msg), GFP_KERNEL);
- msg->cmd = ACCELEROMETER_FACTORY;
- msg->length = 8;
- msg->options = AP2HUB_READ;
- msg->data = chTempBuf[0];
- msg->buffer = chTempBuf;
- msg->free_buffer = 0;
- iRet = ssp_spi_sync(data, msg, 3000);
- if (iRet != SUCCESS) {
- pr_err("[SSP] %s - accel hw selftest Timeout!!\n", __func__);
- return sprintf(buf, "%d,%d,%d,%d\n", -5, 0, 0, 0);
- }
- init_status = chTempBuf[0];
- shift_ratio[0] = (s16)((chTempBuf[2] << 8) + chTempBuf[1]);
- shift_ratio[1] = (s16)((chTempBuf[4] << 8) + chTempBuf[3]);
- shift_ratio[2] = (s16)((chTempBuf[6] << 8) + chTempBuf[5]);
- result = chTempBuf[7];
- pr_info("[SSP] %s - %d, %d, %d, %d, %d\n", __func__,
- init_status, result, shift_ratio[0], shift_ratio[1], shift_ratio[2]);
- return sprintf(buf, "%d,%d.%d,%d.%d,%d.%d\n", result,
- shift_ratio[0] / 10, shift_ratio[0] % 10,
- shift_ratio[1] / 10, shift_ratio[1] % 10,
- shift_ratio[2] / 10, shift_ratio[2] % 10);
- }
- static ssize_t accel_lowpassfilter_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t size)
- {
- int iRet = 0, new_enable = 1;
- struct ssp_data *data = dev_get_drvdata(dev);
- struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
- if (msg == NULL) {
- pr_err("[SSP] %s, failed to alloc memory\n", __func__);
- goto exit;
- }
- if (sysfs_streq(buf, "1"))
- new_enable = 1;
- else if (sysfs_streq(buf, "0"))
- new_enable = 0;
- else
- ssp_dbg("[SSP]: %s - invalid value!\n", __func__);
- msg->cmd = MSG2SSP_AP_SENSOR_LPF;
- msg->length = 1;
- msg->options = AP2HUB_WRITE;
- msg->buffer = (char*) kzalloc(1, GFP_KERNEL);
- if (msg->buffer == NULL) {
- pr_err("[SSP] %s, failed to alloc memory\n", __func__);
- kfree(msg);
- goto exit;
- }
- *msg->buffer = new_enable;
- msg->free_buffer = 1;
- iRet = ssp_spi_async(data, msg);
- if (iRet != SUCCESS)
- pr_err("[SSP] %s - fail %d\n", __func__, iRet);
- else
- pr_info("[SSP] %s - %d\n", __func__, new_enable);
- exit:
- return size;
- }
- static DEVICE_ATTR(name, S_IRUGO, accel_name_show, NULL);
- static DEVICE_ATTR(vendor, S_IRUGO, accel_vendor_show, NULL);
- static DEVICE_ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
- accel_calibration_show, accel_calibration_store);
- static DEVICE_ATTR(raw_data, S_IRUGO, raw_data_read, NULL);
- static DEVICE_ATTR(reactive_alert, S_IRUGO | S_IWUSR | S_IWGRP,
- accel_reactive_alert_show, accel_reactive_alert_store);
- static DEVICE_ATTR(selftest, S_IRUGO, accel_hw_selftest_show, NULL);
- static DEVICE_ATTR(lowpassfilter, S_IWUSR | S_IWGRP,
- NULL, accel_lowpassfilter_store);
- static struct device_attribute *acc_attrs[] = {
- &dev_attr_name,
- &dev_attr_vendor,
- &dev_attr_calibration,
- &dev_attr_raw_data,
- &dev_attr_reactive_alert,
- &dev_attr_selftest,
- &dev_attr_lowpassfilter,
- NULL,
- };
- void initialize_accel_factorytest(struct ssp_data *data)
- {
- sensors_register(data->acc_device, data, acc_attrs,
- "accelerometer_sensor");
- }
- void remove_accel_factorytest(struct ssp_data *data)
- {
- sensors_unregister(data->acc_device, acc_attrs);
- }
|