/* drivers/input/sensors/access/akm09918.c
*
* Copyright (C) 2012-2015 ROCKCHIP.
* Author: luowei <lw@rock-chips.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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 <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/freezer.h>
#include <linux/of_gpio.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include <linux/sensor-dev.h>
#define AKM_SENSOR_INFO_SIZE 2
#define AKM_SENSOR_CONF_SIZE 3
#define SENSOR_DATA_SIZE 9
#define YPR_DATA_SIZE 12
#define RWBUF_SIZE 16
#define ACC_DATA_FLAG 0
#define MAG_DATA_FLAG 1
#define ORI_DATA_FLAG 2
#define AKM_NUM_SENSORS 3
#define ACC_DATA_READY (1 << (ACC_DATA_FLAG))
#define MAG_DATA_READY (1 << (MAG_DATA_FLAG))
#define ORI_DATA_READY (1 << (ORI_DATA_FLAG))
/*Constant definitions of the AK09918.*/
#define AK09918_MEASUREMENT_TIME_US 10000
#define AK09918_MODE_SNG_MEASURE 0x01
#define AK09918_MODE_SELF_TEST 0x10
#define AK09918_MODE_POWERDOWN 0x00
#define AK09918_REG_WIA1 0x00
#define AK09918_REG_WIA2 0x01
#define AK09918_REG_ST1 0x10
#define AK09918_REG_HXL 0x11
#define AK09918_REG_HXH 0x12
#define AK09918_REG_HYL 0x13
#define AK09918_REG_HYH 0x14
#define AK09918_REG_HZL 0x15
#define AK09918_REG_HZH 0x16
#define AK09918_REG_TMPS 0x17
#define AK09918_REG_ST2 0x18
#define AK09918_REG_CNTL1 0x30
#define AK09918_REG_CNTL2 0x31
#define AK09918_REG_CNTL3 0x32
#define AK09918_WIA_VALUE 0x48
#define COMPASS_IOCTL_MAGIC 'c'
/* IOCTLs for AKM library */
#define ECS_IOCTL_WRITE _IOW(COMPASS_IOCTL_MAGIC, 0x01, char*)
#define ECS_IOCTL_READ _IOWR(COMPASS_IOCTL_MAGIC, 0x02, char*)
#define ECS_IOCTL_RESET _IO(COMPASS_IOCTL_MAGIC, 0x03)
#define ECS_IOCTL_SET_MODE _IOW(COMPASS_IOCTL_MAGIC, 0x04, short)
#define ECS_IOCTL_GETDATA _IOR(COMPASS_IOCTL_MAGIC, 0x05, char[SENSOR_DATA_SIZE])
#define ECS_IOCTL_SET_YPR _IOW(COMPASS_IOCTL_MAGIC, 0x06, short[12])
#define ECS_IOCTL_GET_OPEN_STATUS _IOR(COMPASS_IOCTL_MAGIC, 0x07, int)
#define ECS_IOCTL_GET_CLOSE_STATUS _IOR(COMPASS_IOCTL_MAGIC, 0x08, int)
#define ECS_IOCTL_GET_LAYOUT _IOR(COMPASS_IOCTL_MAGIC, 0x09, char)
#define ECS_IOCTL_GET_ACCEL _IOR(COMPASS_IOCTL_MAGIC, 0x0A, short[3])
#define ECS_IOCTL_GET_OUTBIT _IOR(COMPASS_IOCTL_MAGIC, 0x0B, char)
#define ECS_IOCTL_GET_DELAY _IOR(COMPASS_IOCTL_MAGIC, 0x30, short)
#define ECS_IOCTL_GET_PROJECT_NAME _IOR(COMPASS_IOCTL_MAGIC, 0x0D, char[64])
#define ECS_IOCTL_GET_MATRIX _IOR(COMPASS_IOCTL_MAGIC, 0x0E, short [4][3][3])
#define ECS_IOCTL_GET_PLATFORM_DATA _IOR(COMPASS_IOCTL_MAGIC, 0x0E, struct akm_platform_data)
#define ECS_IOCTL_GET_INFO _IOR(COMPASS_IOCTL_MAGIC, 0x27, unsigned char[AKM_SENSOR_INFO_SIZE])
#define ECS_IOCTL_GET_CONF _IOR(COMPASS_IOCTL_MAGIC, 0x28, unsigned char[AKM_SENSOR_CONF_SIZE])
#define AK09918_DEVICE_ID 0x48
static struct i2c_client *this_client;
static struct miscdevice compass_dev_device;
static int g_akm_rbuf_ready;
static int g_akm_rbuf[12];
/****************operate according to sensor chip:start************/
static int sensor_active(struct i2c_client *client, int enable, int rate)
{
struct sensor_private_data *sensor =
(struct sensor_private_data *)i2c_get_clientdata(client);
int result = 0;
if (enable)
sensor->ops->ctrl_data = AK09918_MODE_SNG_MEASURE;
else
sensor->ops->ctrl_data = AK09918_MODE_POWERDOWN;
result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
if (result)
pr_err("%s:fail to active sensor\n", __func__);
return result;
}
static int sensor_init(struct i2c_client *client)
{
struct sensor_private_data *sensor =
(struct sensor_private_data *)i2c_get_clientdata(client);
int result = 0;
char info = 0;
this_client = client;
result = sensor->ops->active(client, 0, 0);
if (result) {
pr_err("%s:line=%d,error\n", __func__, __LINE__);
return result;
}
sensor->status_cur = SENSOR_OFF;
info = sensor_read_reg(client, AK09918_REG_WIA1);
if (info != AK09918_WIA_VALUE) {
pr_err("%s:info=0x%x,it is not %s\n", __func__, info, sensor->ops->name);
return -1;
}
result = misc_register(&compass_dev_device);
if (result < 0) {
pr_err("%s:fail to register misc device %s\n", __func__, compass_dev_device.name);
result = -1;
}
return result;
}
static void compass_report_value(void)
{
struct sensor_private_data *sensor =
(struct sensor_private_data *)i2c_get_clientdata(this_client);
static int flag;
if (!g_akm_rbuf_ready) {
pr_info("g_akm_rbuf not ready..............\n");
return;
}
/* Report magnetic vector information */
if (atomic_read(&sensor->flags.mv_flag) && (g_akm_rbuf[0] & MAG_DATA_READY)) {
/*
*input dev will ignore report data if data value is the same with last_value,
*sample rate will not enough by this way, so just avoid this case
*/
if ((sensor->axis.x == g_akm_rbuf[5]) &&
(sensor->axis.y == g_akm_rbuf[6]) && (sensor->axis.z == g_akm_rbuf[7])) {
if (flag) {
flag = 0;
sensor->axis.x += 1;
sensor->axis.y += 1;
sensor->axis.z += 1;
} else {
flag = 1;
sensor->axis.x -= 1;
sensor->axis.y -= 1;
sensor->axis.z -= 1;
}
} else {
sensor->axis.x = g_akm_rbuf[5];
sensor->axis.y = g_akm_rbuf[6];
sensor->axis.z = g_akm_rbuf[7];
}
input_report_abs(sensor->input_dev, ABS_HAT0X, sensor->axis.x);
input_report_abs(sensor->input_dev, ABS_HAT0Y, sensor->axis.y);
input_report_abs(sensor->input_dev, ABS_BRAKE, sensor->axis.z);
input_report_abs(sensor->input_dev, ABS_HAT1X, g_akm_rbuf[8]);
}
input_sync(sensor->input_dev);
printk("--------ak09918----------\n");
printk("x=%d,y=%d,z=%d\n",sensor->axis.x,sensor->axis.y,sensor->axis.z);
}
static void AKECS_Compat2akm8963(char *buf)
{
int16_t mag_x,mag_y,mag_z;
mag_x = (int16_t)(buf[1] | (buf[2] << 8));
mag_y = (int16_t)(buf[3] | (buf[4] << 8));
mag_z = (int16_t)(buf[5] | (buf[6] << 8));
mag_x /= 4;
buf[1] = mag_x & 0xFF;
buf[2] = (mag_x >> 8) & 0xFF;
mag_y /= 4;
buf[3] = mag_y & 0xFF;
buf[4] = (mag_y >> 8) & 0xFF;
mag_z /= 4;
buf[5] = mag_z & 0xFF;
buf[6] = (mag_z >> 8) & 0xFF;
buf[7] = buf[8];
}
static int sensor_report_value(struct i2c_client *client)
{
struct sensor_private_data *sensor = (struct sensor_private_data *)i2c_get_clientdata(client);
char buffer[SENSOR_DATA_SIZE] = {0};
int ret = 0;
char value = 0;
mutex_lock(&sensor->data_mutex);
compass_report_value();
mutex_unlock(&sensor->data_mutex);
if (sensor->ops->read_len < SENSOR_DATA_SIZE) {
pr_err("%s:length is error,len=%d\n", __func__, sensor->ops->read_len);
return -1;
}
memset(buffer, 0, SENSOR_DATA_SIZE);
/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
do {
*buffer = sensor->ops->read_reg;
ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
if (ret < 0)
return ret;
} while (0);
AKECS_Compat2akm8963(buffer);
mutex_lock(&sensor->data_mutex);
memcpy(sensor->sensor_data, buffer, sensor->ops->read_len);
mutex_unlock(&sensor->data_mutex);
if ((sensor->pdata->irq_enable) && (sensor->ops->int_status_reg >= 0))
value = sensor_read_reg(client, sensor->ops->int_status_
评论0