IIO: Invensense MPU6050/MPU9150 driver submit
Subsystems (3)
PATCH 3/4 IIO: Invensense MPU6050/MPU9150 driver submit
From 0aba028fa8fdd6423b3b86320869a30078b7f684 Mon Sep 17 00:00:00 2001
From: Ge Gao <[email protected]>
Date: Thu, 28 Jun 2012 11:03:44 -0700
Subject: [PATCH 3/4] Invensense MPU6050/MPU9150 driver.
--MPU6050/MPU9150 driver.
--Secondary bus for AKM8975/AKM8963/AKM8972 support. --kernel fix for Kfifo poll support from Jonanthan. --Kfifo bug fix. Need to check available space before store. --add new IIO type for quaternion.
Change-Id I3e182bdd15fc93ce6016bb117abbaa6d3a6b9999
Signed-off-by Ge Gao <[email protected]>
--- drivers/staging/iio/imu/mpu6050/inv_mpu_misc.c | 1712 ++++++++++++++++++++++++ 1 files changed, 1712 insertions(+), 0 deletions(-) create mode 100644 drivers/staging/iio/imu/mpu6050/inv_mpu_misc.c
drivers/staging/iio/imu/mpu6050/inv_mpu_misc.c new file +1712 -0
@@ -0,0 +1,1712 @@ +/* +* Copyright (C) 2012 Invensense, Inc. +* +* 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. +* +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/i2c.h> +#include <linux/err.h> +#include <linux/delay.h> +#include <linux/sysfs.h> +#include <linux/jiffies.h> +#include <linux/irq.h> +#include <linux/interrupt.h> +#include <linux/kfifo.h> +#include <linux/poll.h> +#include <linux/miscdevice.h> +#include <linux/crc32.h> + +#include "inv_mpu_iio.h" +/* DMP defines */ +#define DMP_ORIENTATION_TIME 500 +#define DMP_ORIENTATION_ANGLE 60 +#define DMP_DEFAULT_FIFO_RATE 200 +#define DMP_TAP_SCALE (767603923 / 5) +#define DMP_MULTI_SHIFT 30 +#define DMP_MULTI_TAP_TIME 500 +#define DMP_SHAKE_REJECT_THRESH 100 +#define DMP_SHAKE_REJECT_TIME 10 +#define DMP_SHAKE_REJECT_TIMEOUT 10 +#define DMP_ANGLE_SCALE 15 +#define DMP_PRECISION 1000 +#define DMP_MAX_DIVIDER 4 +#define DMP_MAX_MIN_TAPS 4 +#define DMP_IMAGE_CRC_VALUE 0x6590dad6 +#define DMP_IMAGE_SIZE 3065 + +/*--- Test parameters defaults --- */ +#define DEF_OLDEST_SUPP_PROD_REV 8 +#define DEF_OLDEST_SUPP_SW_REV 2 + +/* sample rate */ +#define DEF_SELFTEST_SAMPLE_RATE 0 +/* LPF parameter */ +#define DEF_SELFTEST_LPF_PARA 1 +/* full scale setting dps */ +#define DEF_SELFTEST_GYRO_FULL_SCALE (0 << 3) +#define DEF_SELFTEST_ACCL_FULL_SCALE (2 << 3) +#define DEF_SELFTEST_GYRO_SENS (32768 / 250) +/* wait time before collecting data */ +#define DEF_GYRO_WAIT_TIME 50 +#define DEF_ST_STABLE_TIME 200 +#define DEF_GYRO_PACKET_THRESH DEF_GYRO_WAIT_TIME +#define DEF_GYRO_THRESH 10 +#define DEF_GYRO_SCALE 131 +#define DEF_ST_PRECISION 1000 +#define DEF_ST_ACCL_FULL_SCALE 8000UL +#define DEF_ST_SCALE (1L << 15) +#define DEF_ST_TRY_TIMES 2 +#define DEF_ST_COMPASS_RESULT_SHIFT 2 +#define DEF_ST_ACCEL_RESULT_SHIFT 1 + +#define DEF_ST_COMPASS_WAIT_MIN (10 * 1000) +#define DEF_ST_COMPASS_WAIT_MAX (15 * 1000) +#define DEF_ST_COMPASS_TRY_TIMES 10 +#define DEF_ST_COMPASS_8963_SHIFT 2 + +#define X 0 +#define Y 1 +#define Z 2 +/*---- MPU6050 notable product revisions ----*/ +#define MPU_PRODUCT_KEY_B1_E1_5 105 +#define MPU_PRODUCT_KEY_B2_F1 431 +/* accelerometer Hw self test min and max bias shift (mg) */ +#define DEF_ACCEL_ST_SHIFT_MIN 300 +#define DEF_ACCEL_ST_SHIFT_MAX 950 + +#define DEF_ACCEL_ST_SHIFT_DELTA 140 +#define DEF_GYRO_CT_SHIFT_DELTA 140 +/* gyroscope Coriolis self test min and max bias shift (dps) */ +#define DEF_GYRO_CT_SHIFT_MIN 10 +#define DEF_GYRO_CT_SHIFT_MAX 105 + +static struct test_setup_t test_setup = { + .gyro_sens =DEF_SELFTEST_GYRO_SENS , + .sample_rate =DEF_SELFTEST_SAMPLE_RATE , + .lpf =DEF_SELFTEST_LPF_PARA , + .fsr =DEF_SELFTEST_GYRO_FULL_SCALE , + .accl_fs =DEF_SELFTEST_ACCL_FULL_SCALE +}; + +/* NOTE: product entries are in chronological order */ +static const struct prod_rev_map_t prod_rev_map[] = { +/* prod_ver = 0 */ + {MPL_PROD_KEY (0 ,1 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,2 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,3 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,4 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,5 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,6 ),MPU_SILICON_REV_A2 ,131 ,16384 },/* (A2/C2-1) */ +/* prod_ver = 1, forced to 0 for MPU6050 A2 */ + {MPL_PROD_KEY (0 ,7 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,8 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,9 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,10 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,11 ),MPU_SILICON_REV_A2 ,131 ,16384 },/* (A2/D2-1) */ + {MPL_PROD_KEY (0 ,12 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,13 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,14 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,15 ),MPU_SILICON_REV_A2 ,131 ,16384 }, + {MPL_PROD_KEY (0 ,27 ),MPU_SILICON_REV_A2 ,131 ,16384 },/* (A2/D4) */ +/* prod_ver = 1 */ + {MPL_PROD_KEY (1 ,16 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/D2-1) */ + {MPL_PROD_KEY (1 ,17 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/D2-2) */ + {MPL_PROD_KEY (1 ,18 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/D2-3) */ + {MPL_PROD_KEY (1 ,19 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/D2-4) */ + {MPL_PROD_KEY (1 ,20 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/D2-5) */ + {MPL_PROD_KEY (1 ,28 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/D4) */ + {MPL_PROD_KEY (1 ,1 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/E1-1) */ + {MPL_PROD_KEY (1 ,2 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/E1-2) */ + {MPL_PROD_KEY (1 ,3 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/E1-3) */ + {MPL_PROD_KEY (1 ,4 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/E1-4) */ + {MPL_PROD_KEY (1 ,5 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/E1-5) */ + {MPL_PROD_KEY (1 ,6 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B1/E1-6) */ +/* prod_ver = 2 */ + {MPL_PROD_KEY (2 ,7 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/E1-1) */ + {MPL_PROD_KEY (2 ,8 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/E1-2) */ + {MPL_PROD_KEY (2 ,9 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/E1-3) */ + {MPL_PROD_KEY (2 ,10 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/E1-4) */ + {MPL_PROD_KEY (2 ,11 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/E1-5) */ + {MPL_PROD_KEY (2 ,12 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/E1-6) */ + {MPL_PROD_KEY (2 ,29 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/D4) */ +/* prod_ver = 3 */ + {MPL_PROD_KEY (3 ,30 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B2/E2) */ +/* prod_ver = 4 */ + {MPL_PROD_KEY (4 ,31 ),MPU_SILICON_REV_B1 ,131 ,8192 },/* (B2/F1) */ + {MPL_PROD_KEY (4 ,1 ),MPU_SILICON_REV_B1 ,131 ,8192 },/* (B3/F1) */ + {MPL_PROD_KEY (4 ,3 ),MPU_SILICON_REV_B1 ,131 ,8192 },/* (B4/F1) */ +/* prod_ver = 5 */ + {MPL_PROD_KEY (5 ,3 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B4/F1) */ +/* prod_ver = 6 */ + {MPL_PROD_KEY (6 ,19 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B5/E2) */ +/* prod_ver = 7 */ + {MPL_PROD_KEY (7 ,19 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B5/E2) */ +/* prod_ver = 8 */ + {MPL_PROD_KEY (8 ,19 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B5/E2) */ +/* prod_ver = 9 */ + {MPL_PROD_KEY (9 ,19 ),MPU_SILICON_REV_B1 ,131 ,16384 },/* (B5/E2) */ +/* prod_ver = 10 */ + {MPL_PROD_KEY (10 ,19 ),MPU_SILICON_REV_B1 ,131 ,16384 }/* (B5/E2) */ +}; + +/* +* List of product software revisions +* +* NOTE : +* software revision 0 falls back to the old detection method +* based off the product version and product revision per the +* table above +*/ +static const struct prod_rev_map_t sw_rev_map[] = { + {0 ,0 ,0 ,0 }, + {1 ,MPU_SILICON_REV_B1 ,131 ,8192 },/* rev C */ + {2 ,MPU_SILICON_REV_B1 ,131 ,16384 }/* rev D */ +}; + +static const int accl_st_tb[31 ] = { +340 ,351 ,363 ,375 ,388 ,401 ,414 ,428 , +443 ,458 ,473 ,489 ,506 ,523 ,541 ,559 , +578 ,597 ,617 ,638 ,660 ,682 ,705 ,729 , +753 ,779 ,805 ,832 ,860 ,889 ,919 }; +static const int gyro_6050_st_tb[31 ] = { +3275 ,3425 ,3583 ,3748 ,3920 ,4100 ,4289 ,4486 , +4693 ,4909 ,5134 ,5371 ,5618 ,5876 ,6146 ,6429 , +6725 ,7034 ,7358 ,7696 ,8050 ,8421 ,8808 ,9213 , +9637 ,10080 ,10544 ,11029 ,11537 ,12067 ,12622 }; + +int mpu_memory_write (struct i2c_adapter *i2c_adap, +unsigned char mpu_addr, +unsigned short mem_addr, +unsigned int len,unsigned char const *data) +{ +unsigned char bank[2 ]; +unsigned char addr[2 ]; +unsigned char buf[513 ]; + +struct i2c_msg msgs[3 ]; +int res; + +if (!data || !i2c_adap) +return -EINVAL ; + +if (len >= (sizeof (buf) -1 )) +return -ENOMEM ; + + bank[0 ] =REG_BANK_SEL ; + bank[1 ] = mem_addr >>8 ; + + addr[0 ] =REG_MEM_START_ADDR ; + addr[1 ] = mem_addr &0xFF ; + + buf[0 ] =REG_MEM_RW ; +memcpy (buf +1 , data, len); + +/* write message */ + msgs[0 ].addr = mpu_addr; + msgs[0 ].flags =0 ; + msgs[0 ].buf = bank; + msgs[0 ].len =sizeof (bank); + + msgs[1 ].addr = mpu_addr; + msgs[1 ].flags =0 ; + msgs[1 ].buf = addr; + msgs[1 ].len =sizeof (addr); + + msgs[2 ].addr = mpu_addr; + msgs[2 ].flags =0 ; + msgs[2 ].buf = (unsigned char *)buf; + msgs[2 ].len = len +1 ; + + res =i2c_transfer (i2c_adap, msgs,3 ); +if (res !=3 ) { +if (res >=0 ) + res = -EIO ; +return res; + }else { +return 0 ; + } +} + +int mpu_memory_read (struct i2c_adapter *i2c_adap, +unsigned char mpu_addr, +unsigned short mem_addr, +unsigned int len,unsigned char *data) +{ +unsigned char bank[2 ]; +unsigned char addr[2 ]; +unsigned char buf; + +struct i2c_msg msgs[4 ]; +int res; + +if (!data || !i2c_adap) +return -EINVAL ; + + bank[0 ] =REG_BANK_SEL ; + bank[1 ] = mem_addr >>8 ; + + addr[0 ] =REG_MEM_START_ADDR ; + addr[1 ] = mem_addr &0xFF ; + + buf =REG_MEM_RW ; + +/* write message */ + msgs[0 ].addr = mpu_addr; + msgs[0 ].flags =0 ; + msgs[0 ].buf = bank; + msgs[0 ].len =sizeof (bank); + + msgs[1 ].addr = mpu_addr; + msgs[1 ].flags =0 ; + msgs[1 ].buf = addr; + msgs[1 ].len =sizeof (addr); + + msgs[2 ].addr = mpu_addr; + msgs[2 ].flags =0 ; + msgs[2 ].buf = &buf; + msgs[2 ].len =1 ; + + msgs[3 ].addr = mpu_addr; + msgs[3 ].flags =I2C_M_RD ; + msgs[3 ].buf = data; + msgs[3 ].len = len; + + res =i2c_transfer (i2c_adap, msgs,4 ); +if (res !=4 ) { +if (res >=0 ) + res = -EIO ; +return res; + }else { +return 0 ; + } +} + +/** +* @internal +* @brief Inverse lookup of the index of an MPL product key . +* @param key +* the MPL product indentifier also referred to as 'key'. +* @return the index position of the key in the array. +*/ +static short index_of_key (unsigned short key) +{ +int i; +for (i =0 ; i <NUM_OF_PROD_REVS ; i++) +if (prod_rev_map[i].mpl_product_key == key) +return (short )i; +return -EINVAL ; +} + +int inv_get_silicon_rev_mpu6050 (struct inv_mpu_iio_s *st) +{ +int result; +struct inv_reg_map_s *reg; +unsigned char prod_ver =0x00 , prod_rev =0x00 ; +struct prod_rev_map_t *p_rev; +unsigned char bank = + (BIT_PRFTCH_EN |BIT_CFG_USER_BANK |MPU_MEM_OTP_BANK_0 ); +unsigned short mem_addr = ((bank <<8 ) |MEM_ADDR_PROD_REV ); +unsigned short key; +unsigned char regs[5 ]; +unsigned short sw_rev; +short index; +struct inv_chip_info_s *chip_info = &st->chip_info; + reg = &st->reg; + + result =inv_i2c_read (st,REG_PRODUCT_ID ,1 , &prod_ver); +if (result) +return result; + prod_ver &=0xf ; +/*memory read need more time after power up */ +msleep (POWER_UP_TIME ); + result =mpu_memory_read (st->sl_handle, st->i2c_addr, mem_addr, +1 , &prod_rev); +if (result) +return result; + prod_rev >>=2 ; +/* clean the prefetch and cfg user bank bits */ + result =inv_i2c_single_write (st, reg->bank_sel,0 ); +if (result) +return result; +/* get the software-product version, read from XA_OFFS_L */ + result =inv_i2c_read (st,REG_XA_OFFS_L_TC , +SOFT_PROD_VER_BYTES , regs); +if (result) +return result; + + sw_rev = (regs[4 ] &0x01 ) <<2 |/* 0x0b, bit 0 */ + (regs[2 ] &0x01 ) <<1 |/* 0x09, bit 0 */ + (regs[0 ] &0x01 );/* 0x07, bit 0 */ +/* if 0, use the product key to determine the type of part */ +if (sw_rev ==0 ) { + key =MPL_PROD_KEY (prod_ver, prod_rev); +if (key ==0 ) +return -EINVAL ; + index =index_of_key (key); +if (index == -EINVAL || index >=NUM_OF_PROD_REVS ) +return -EINVAL ; +/* check MPL is compiled for this device */ +if (prod_rev_map[index].silicon_rev !=MPU_SILICON_REV_B1 ) +return -EINVAL ; + p_rev = (struct prod_rev_map_t *)&prod_rev_map[index]; +/* if valid, use the software product key */ + }else if (sw_rev <ARRAY_SIZE (sw_rev_map)) { + p_rev = (struct prod_rev_map_t *)&sw_rev_map[sw_rev]; + }else { +return -EINVAL ; + } + chip_info->product_id = prod_ver; + chip_info->product_revision = prod_rev; + chip_info->silicon_revision = p_rev->silicon_rev; + chip_info->software_revision = sw_rev; + chip_info->gyro_sens_trim = p_rev->gyro_trim; + chip_info->accl_sens_trim = p_rev->accel_trim; +if (chip_info->accl_sens_trim ==0 ) + chip_info->accl_sens_trim =DEFAULT_ACCL_TRIM ; + chip_info->multi =DEFAULT_ACCL_TRIM / chip_info->accl_sens_trim; +if (chip_info->multi !=1 ) +pr_info ("multi is %d\n" , chip_info->multi); +return result; +} + +/** +* @internal +* @brief read the accelerometer hardware self-test bias shift calculated +* during final production test and stored in chip non-volatile memory. +* @param st +* main data structure. +* @param ct_shift_prod +* A pointer to an array of 3 elements to hold the values +* for production hardware self-test bias shifts returned to the +* user. +* @return 0 on success, or a non-zero error code otherwise. +*/ +static int read_accel_hw_self_test_prod_shift (struct inv_mpu_iio_s *st, +int *st_prod) +{ +unsigned char regs[4 ]; +unsigned char shift_code[3 ]; +int result, i; + st_prod[0 ] =0 ; + st_prod[1 ] =0 ; + st_prod[2 ] =0 ; + result =inv_i2c_read (st,REG_ST_GCT_X ,ARRAY_SIZE (regs), regs); +if (result) +return result; +if ((0 == regs[0 ]) && (0 == regs[1 ]) && + (0 == regs[2 ]) && (0 == regs[3 ])) +return -EINVAL ; + shift_code[X ] = ((regs[0 ] &0xE0 ) >>3 ) | ((regs[3 ] &0x30 ) >>4 ); + shift_code[Y ] = ((regs[1 ] &0xE0 ) >>3 ) | ((regs[3 ] &0x0C ) >>2 ); + shift_code[Z ] = ((regs[2 ] &0xE0 ) >>3 ) | (regs[3 ] &0x03 ); +for (i =0 ; i <3 ; i++) { +if (shift_code[i] !=0 ) + st_prod[i] = test_setup.accl_sens[i]* + accl_st_tb[shift_code[i] -1 ]; + } + +return 0 ; +} +/** +* @brief check accel self test. +* this function returns zero as success. A non-zero +* return value indicates failure in self test. +* @param *st main data structure. +* @param *reg_avg average value of normal test. +* @param *st_avg average value of self test +*/ +static int inv_check_accl_self_test (struct inv_mpu_iio_s *st, +int *reg_avg,int *st_avg){ +int gravity, reg_z_avg, g_z_sign, fs, j, ret_val; +int tmp1; +int st_shift_prod[THREE_AXIS ], st_shift_cust[THREE_AXIS ]; +int st_shift_ratio[THREE_AXIS ]; +if (st->chip_info.software_revision <DEF_OLDEST_SUPP_SW_REV && + st->chip_info.product_revision <DEF_OLDEST_SUPP_PROD_REV ) +return 0 ; + fs =DEF_ST_ACCL_FULL_SCALE ;/* assume +/- 2 mg as typical */ + g_z_sign =1 ; + ret_val =0 ; + test_setup.accl_sens[X ] = (unsigned int )(DEF_ST_SCALE * +DEF_ST_PRECISION / fs); + test_setup.accl_sens[Y ] = (unsigned int )(DEF_ST_SCALE * +DEF_ST_PRECISION / fs); + test_setup.accl_sens[Z ] = (unsigned int )(DEF_ST_SCALE * +DEF_ST_PRECISION / fs); + +if (MPL_PROD_KEY (st->chip_info.product_id, + st->chip_info.product_revision) == +MPU_PRODUCT_KEY_B1_E1_5 ) { +/* half sensitivity Z accelerometer parts */ + test_setup.accl_sens[Z ] /=2 ; + }else { +/* half sensitivity X, Y, Z accelerometer parts */ + test_setup.accl_sens[X ] /= st->chip_info.multi; + test_setup.accl_sens[Y ] /= st->chip_info.multi; + test_setup.accl_sens[Z ] /= st->chip_info.multi; + } + gravity = test_setup.accl_sens[Z ]; + reg_z_avg = reg_avg[Z ] - g_z_sign * gravity*DEF_ST_PRECISION ; +read_accel_hw_self_test_prod_shift (st, st_shift_prod); +for (j =0 ; j <3 ; j++) { + st_shift_cust[j] =abs (reg_avg[j] - st_avg[j]); +if (st_shift_prod[j]) { + tmp1 = st_shift_prod[j]/DEF_ST_PRECISION ; + st_shift_ratio[j] = st_shift_cust[j]/tmp1 + -DEF_ST_PRECISION ; +if (st_shift_ratio[j] >DEF_ACCEL_ST_SHIFT_DELTA ) + ret_val |=1 << j; +if (st_shift_ratio[j] < -DEF_ACCEL_ST_SHIFT_DELTA ) + ret_val |=1 << j; + }else { +if (st_shift_cust[j] < +DEF_ACCEL_ST_SHIFT_MIN *gravity) + ret_val |=1 << j; +if (st_shift_cust[j] > +DEF_ACCEL_ST_SHIFT_MAX *gravity) + ret_val |=1 << j; + } + } + +return ret_val; +} +/** +* @brief check 6050 gyro self test. +* this function returns zero as success. A non-zero +* return value indicates failure in self test. +* @param st main data structure. +* @param *reg_avg average value of normal test. +* @param *st_avg average value of self test +*/ +static int inv_check_6050_gyro_self_test (struct inv_mpu_iio_s *st, +int *reg_avg,int *st_avg){ +int result; +int ret_val; +int ct_shift_prod[3 ], st_shift_cust[3 ], st_shift_ratio[3 ], i; +unsigned char regs[3 ]; +if (st->chip_info.software_revision <DEF_OLDEST_SUPP_SW_REV && + st->chip_info.product_revision <DEF_OLDEST_SUPP_PROD_REV ) +return 0 ; + + ret_val =0 ; + result =inv_i2c_read (st,REG_ST_GCT_X ,3 , regs); +if (result) +return result; + regs[X ] &=0x1f ; + regs[Y ] &=0x1f ; + regs[Z ] &=0x1f ; + +for (i =0 ; i <3 ; i++) { +if (regs[i] !=0 ) + ct_shift_prod[i] = gyro_6050_st_tb[regs[i] -1 ]; +else + ct_shift_prod[i] =0 ; + } +for (i =0 ; i <3 ; i++) { + st_shift_cust[i] =abs (reg_avg[i] - st_avg[i]); +if (ct_shift_prod[i]) { + st_shift_ratio[i] = st_shift_cust[i] / + ct_shift_prod[i] -DEF_ST_PRECISION ; +if (st_shift_ratio[i] >DEF_GYRO_CT_SHIFT_DELTA ) + ret_val |=1 << i; +if (st_shift_ratio[i] < -DEF_GYRO_CT_SHIFT_DELTA ) + ret_val |=1 << i; + }else { +if (st_shift_cust[i] <DEF_ST_PRECISION * +DEF_GYRO_CT_SHIFT_MIN * test_setup.gyro_sens) + ret_val |=1 << i; +if (st_shift_cust[i] >DEF_ST_PRECISION * +DEF_GYRO_CT_SHIFT_MAX * test_setup.gyro_sens) + ret_val |=1 << i; + } + } +for (i =0 ; i <3 ; i++) { +if (abs (reg_avg[i]) *4 >20 *2 * +DEF_ST_PRECISION *DEF_GYRO_SCALE ) + ret_val |= (1 << i); + } + +return ret_val; +} + +/** +* inv_do_test() - do the actual test of self testing +*/ +int inv_do_test (struct inv_mpu_iio_s *st,int self_test_flag, +int *gyro_result,int *accl_result) +{ +struct inv_reg_map_s *reg; +int result, i, j, packet_size; +unsigned char data[BYTES_PER_SENSOR *2 ], has_accl; +int fifo_count, packet_count, ind; + + reg = &st->reg; + has_accl =1 ; + packet_size =BYTES_PER_SENSOR *(1 + has_accl); + + result =inv_i2c_single_write (st, reg->int_enable,0 ); +if (result) +return result; +/* disable the sensor output to FIFO */ + result =inv_i2c_single_write (st, reg->fifo_en,0 ); +if (result) +return result; +/* disable fifo reading */ + result =inv_i2c_single_write (st, reg->user_ctrl,0 ); +if (result) +return result; +/* clear FIFO */ + result =inv_i2c_single_write (st, reg->user_ctrl,BIT_FIFO_RST ); +if (result) +return result; +/* setup parameters */ + result =inv_i2c_single_write (st, reg->lpf, test_setup.lpf); +if (result) +return result; + result =inv_i2c_single_write (st, reg->sample_rate_div, + test_setup.sample_rate); +if (result) +return result; + result =inv_i2c_single_write (st, reg->gyro_config, + self_test_flag | test_setup.fsr); +if (result) +return result; +if (has_accl) { + result =inv_i2c_single_write (st, reg->accl_config, + self_test_flag | test_setup.accl_fs); +if (result) +return result; + } +/*wait for the output to stable*/ +if (self_test_flag) +msleep (DEF_ST_STABLE_TIME ); + +/* enable FIFO reading */ + result =inv_i2c_single_write (st, reg->user_ctrl,BIT_FIFO_EN ); +if (result) +return result; +/* enable sensor output to FIFO */ + result =inv_i2c_single_write (st, reg->fifo_en,BITS_GYRO_OUT + | (has_accl <<3 )); +if (result) +return result; +mdelay (DEF_GYRO_WAIT_TIME ); +/* stop sending data to FIFO */ + result =inv_i2c_single_write (st, reg->fifo_en,0 ); +if (result) +return result; + result =inv_i2c_read (st, reg->fifo_count_h,2 , data); +if (result) +return result; + fifo_count = (data[0 ] <<8 ) + data[1 ]; + packet_count = fifo_count/packet_size; +for (i =0 ; i <3 ; i++) { + gyro_result[i] =0 ; + accl_result[i] =0 ; + } +if (abs (packet_count -DEF_GYRO_PACKET_THRESH ) >DEF_GYRO_THRESH ) +return -EAGAIN ; + +for (i =0 ; i < packet_count; i++) { +/* getting FIFO data */ + result =inv_i2c_read (st, reg->fifo_r_w, + packet_size, data); +if (result) +return result; + ind =0 ; +if (has_accl) { +for (j =0 ; j <THREE_AXIS ; j++) + accl_result[j] += + (short )be16_to_cpup ((__be16 + *)(&data[ind +2 * j])); + ind +=BYTES_PER_SENSOR ; + } +for (j =0 ; j <THREE_AXIS ; j++) + gyro_result[j] += + (short )be16_to_cpup ((__be16 *)(&data[ind +2 * j])); + } + + gyro_result[0 ] = gyro_result[0 ] *DEF_ST_PRECISION / packet_count; + gyro_result[1 ] = gyro_result[1 ] *DEF_ST_PRECISION / packet_count; + gyro_result[2 ] = gyro_result[2 ] *DEF_ST_PRECISION / packet_count; +if (has_accl) { + accl_result[0 ] = + accl_result[0 ] *DEF_ST_PRECISION / packet_count; + accl_result[1 ] = + accl_result[1 ] *DEF_ST_PRECISION / packet_count; + accl_result[2 ] = + accl_result[2 ] *DEF_ST_PRECISION / packet_count; + } + +return 0 ; +} + +/** +* inv_recover_setting() recover the old settings after everything is done +*/ + +static void inv_recover_setting (struct inv_mpu_iio_s *st) +{ +struct inv_reg_map_s *reg; +int data; +struct iio_dev *indio =iio_priv_to_dev (st); + + reg = &st->reg; +set_inv_enable (indio, st->chip_config.enable); +inv_i2c_single_write (st, reg->gyro_config, + st->chip_config.fsr <<GYRO_CONFIG_FSR_SHIFT ); +inv_i2c_single_write (st, reg->lpf, st->chip_config.lpf); + data =ONE_K_HZ /st->chip_config.fifo_rate -1 ; +inv_i2c_single_write (st, reg->sample_rate_div, data); +inv_i2c_single_write (st, reg->accl_config, + (st->chip_config.accl_fs << +ACCL_CONFIG_FSR_SHIFT )); + st->set_power_state (st, !st->chip_config.is_asleep); +} + +static int inv_check_compass_self_test (struct inv_mpu_iio_s *st) +{ +int result; +unsigned char data[6 ]; +unsigned char counter, cntl; +short x, y, z; +unsigned char *sens; + sens = st->chip_info.compass_sens; + +/*set to bypass mode */ + result =inv_i2c_single_write (st,REG_INT_PIN_CFG , + st->plat_data.int_config |BIT_BYPASS_EN ); +if (result) { + result =inv_i2c_single_write (st,REG_INT_PIN_CFG , + st->plat_data.int_config); +return result; + } +/*set to power down mode */ + result =inv_secondary_write (REG_AKM_MODE ,DATA_AKM_MODE_PW_DN ); +if (result) + goto AKM_fail; + +/*write 1 to ASTC register */ + result =inv_secondary_write (REG_AKM_ST_CTRL ,DATA_AKM_SELF_TEST ); +if (result) + goto AKM_fail; +/*set self test mode */ + result =inv_secondary_write (REG_AKM_MODE ,DATA_AKM_MODE_PW_ST ); +if (result) + goto AKM_fail; + counter =DEF_ST_COMPASS_TRY_TIMES ; +while (counter >0 ) { +usleep_range (DEF_ST_COMPASS_WAIT_MIN ,DEF_ST_COMPASS_WAIT_MAX ); + result =inv_secondary_read (REG_AKM_STATUS ,1 , data); +if (result) + goto AKM_fail; +if ((data[0 ] &DATA_AKM_DRDY ) ==0 ) + counter--; +else + counter =0 ; + } +if ((data[0 ] &DATA_AKM_DRDY ) ==0 ) { + result = -EINVAL ; + goto AKM_fail; + } + result =inv_secondary_read (REG_AKM_MEASURE_DATA , +BYTES_PER_SENSOR , data); +if (result) + goto AKM_fail; + + x =le16_to_cpup ((__le16 *)(&data[0 ])); + y =le16_to_cpup ((__le16 *)(&data[2 ])); + z =le16_to_cpup ((__le16 *)(&data[4 ])); + x = ((x * (sens[0 ] +128 )) >>8 ); + y = ((y * (sens[1 ] +128 )) >>8 ); + z = ((z * (sens[2 ] +128 )) >>8 ); +if (COMPASS_ID_AK8963 == st->plat_data.sec_slave_id) { + result =inv_secondary_read (REG_AKM8963_CNTL1 ,1 , &cntl); +if (result) + goto AKM_fail; +if (0 == (cntl &DATA_AKM8963_BIT )) { + x <<=DEF_ST_COMPASS_8963_SHIFT ; + y <<=DEF_ST_COMPASS_8963_SHIFT ; + z <<=DEF_ST_COMPASS_8963_SHIFT ; + } + } + result = -EINVAL ; +if (x > st->compass_st_upper[X ] || x < st->compass_st_lower[X ]) + goto AKM_fail; +if (y > st->compass_st_upper[Y ] || y < st->compass_st_lower[Y ]) + goto AKM_fail; +if (z > st->compass_st_upper[Z ] || z < st->compass_st_lower[Z ]) + goto AKM_fail; + result =0 ; +AKM_fail: +/*write 0 to ASTC register */ + result |=inv_secondary_write (REG_AKM_ST_CTRL ,0 ); +/*set to power down mode */ + result |=inv_secondary_write (REG_AKM_MODE ,DATA_AKM_MODE_PW_DN ); +/*restore to non-bypass mode */ + result |=inv_i2c_single_write (st,REG_INT_PIN_CFG , + st->plat_data.int_config); +return result; +} + +static int inv_power_up_self_test (struct inv_mpu_iio_s *st) +{ +int result; + result =inv_i2c_single_write (st, st->reg.pwr_mgmt_1,INV_CLK_PLL ); +if (result) +return result; +msleep (POWER_UP_TIME ); + result =inv_i2c_single_write (st, st->reg.pwr_mgmt_2,0 ); +if (result) +return result; +msleep (SENSOR_UP_TIME ); + +return 0 ; +} + +/** +* inv_hw_self_test() - main function to do hardware self test +*/ +int inv_hw_self_test (struct inv_mpu_iio_s *st) +{ +int result; +int gyro_bias_st[THREE_AXIS ], gyro_bias_regular[THREE_AXIS ]; +int accl_bias_st[THREE_AXIS ], accl_bias_regular[THREE_AXIS ]; +int test_times; +char compass_result, accel_result, gyro_result; +if (st->chip_config.is_asleep || st->chip_config.lpa_mode) { + result =inv_power_up_self_test (st); +if (result) +return result; + } + compass_result =0 ; + accel_result =0 ; + gyro_result =0 ; + test_times =DEF_ST_TRY_TIMES ; +while (test_times >0 ) { + result =inv_do_test (st,0 , gyro_bias_regular, + accl_bias_regular); +if (result == -EAGAIN ) + test_times--; +else + test_times =0 ; + } +if (result) + goto test_fail; + + test_times =DEF_ST_TRY_TIMES ; +while (test_times >0 ) { + result =inv_do_test (st,BITS_SELF_TEST_EN , gyro_bias_st, + accl_bias_st); +if (result == -EAGAIN ) + test_times--; +else +break ; + } +if (result) + goto test_fail; +if (st->chip_config.has_compass) + compass_result = !inv_check_compass_self_test (st); + accel_result = !inv_check_accl_self_test (st, + accl_bias_regular, accl_bias_st); + gyro_result = !inv_check_6050_gyro_self_test (st, + gyro_bias_regular, gyro_bias_st); + +test_fail: +inv_recover_setting (st); + +return (compass_result<<DEF_ST_COMPASS_RESULT_SHIFT ) | + (accel_result<<DEF_ST_ACCEL_RESULT_SHIFT ) | gyro_result; +} + +static int inv_load_firmware (struct inv_mpu_iio_s *st, +unsigned char *data,int size) +{ +int bank, write_size; +int result; +unsigned short memaddr; + +/* Write and verify memory */ +for (bank =0 ; size >0 ; bank++, + size -= write_size, + data += write_size) { +if (size >MPU_MEM_BANK_SIZE ) + write_size =MPU_MEM_BANK_SIZE ; +else + write_size = size; + + memaddr = ((bank <<8 ) |0x00 ); + + result =mem_w (memaddr, write_size, data); +if (result) +return result; + } +return 0 ; +} + +static int inv_verify_firmware (struct inv_mpu_iio_s *st, +unsigned char *data,int size) +{ +int bank, write_size; +int result; +unsigned short memaddr; +unsigned char firmware[MPU_MEM_BANK_SIZE ]; + +/* Write and verify memory */ +for (bank =0 ; size >0 ; bank++, + size -= write_size, + data += write_size) { +if (size >MPU_MEM_BANK_SIZE ) + write_size =MPU_MEM_BANK_SIZE ; +else + write_size = size; + + memaddr = ((bank <<8 ) |0x00 ); + result =mpu_memory_read (st->sl_handle, + st->i2c_addr, memaddr, write_size, firmware); +if (result) +return result; +if (0 !=memcmp (firmware, data, write_size)) +return -EINVAL ; + } +return 0 ; +} + +static int inv_set_fifo_div (struct inv_mpu_iio_s *st, +unsigned short fifoRate) +{ +unsigned char regs[2 ]; +int result =0 ; +/*For some reason DINAC4 is defined as 0xb8, but DINBC4 is not*/ +const unsigned char regs_end[12 ] = {DINAFE ,DINAF2 ,DINAAB ,0xc4 , +DINAAA ,DINAF1 ,DINADF ,DINADF , +0xbb ,0xaf ,DINADF ,DINADF }; + + regs[0 ] = (unsigned char )((fifoRate >>8 ) &0xff ); + regs[1 ] = (unsigned char )(fifoRate &0xff ); + result =mem_w_key (KEY_D_0_22 ,ARRAY_SIZE (regs), regs); +if (result) +return result; + +/*Modify the FIFO handler to reset the tap/orient interrupt flags*/ +/* each time the FIFO handler runs*/ + result =mem_w_key (KEY_CFG_6 ,ARRAY_SIZE (regs_end), regs_end); + +return result; +} + +int inv_send_quaternion (struct inv_mpu_iio_s *st,bool on) +{ +const unsigned char regs_on[] = {DINBC0 ,DINBC2 , +DINBC4 ,DINBC6 }; +const unsigned char regs_off[] = {DINA80 ,DINA80 , +DINA80 ,DINA80 }; +const unsigned char *regs; +unsigned char result; +if (on) + regs = regs_on; +else + regs = regs_off; + result =mem_w_key (KEY_CFG_LP_QUAT ,ARRAY_SIZE (regs_on), regs); +return result; +} + +int inv_set_display_orient_interrupt_dmp (struct inv_mpu_iio_s *st, +bool on) +{ +/*Turn on the display orientation interrupt in the DMP*/ +int result; +unsigned char regs[] = {0xd8 }; + +if (on) + regs[0 ] =0xd9 ; + result =mem_w_key (KEY_CFG_DISPLAY_ORIENT_INT ,1 , regs); +return result; +} + +int inv_set_fifo_rate (struct inv_mpu_iio_s *st,unsigned short fifo_rate) +{ +unsigned char divider; +int result; + + divider = (unsigned char )(ONE_K_HZ / fifo_rate) -1 ; +if (divider >DMP_MAX_DIVIDER ) { + st->sample_divider =DMP_MAX_DIVIDER ; + st->fifo_divider = + (unsigned char )(DMP_DEFAULT_FIFO_RATE / fifo_rate) -1 ; + }else { + st->sample_divider = divider; + st->fifo_divider =0 ; + } + + result =inv_set_fifo_div (st, st->fifo_divider); +return result; +} + +static int inv_set_tap_interrupt_dmp (struct inv_mpu_iio_s *st, +unsigned char on) +{ +int result; +unsigned char regs[] = {0 }; + +if (on) + regs[0 ] =0xf8 ; +else + regs[0 ] =DINAD8 ; + result =mem_w_key (KEY_CFG_20 ,ARRAY_SIZE (regs), regs); +if (result) +return result; +return result; +} + +static int inv_set_orientation_interrupt_dmp (struct inv_mpu_iio_s *st, +unsigned char on) +{ +int result; +unsigned char regs[2 ]; +if (on) { + regs[0 ] =DINBF8 ; + regs[1 ] =DINBF8 ; + }else { + regs[0 ] =DINAD8 ; + regs[1 ] =DINAD8 ; + } + result =mem_w_key (KEY_CFG_ORIENT_IRQ_1 ,ARRAY_SIZE (regs), regs); +if (result) +return result; +return result; +} + +int inv_set_tap_threshold_dmp (struct inv_mpu_iio_s *st, +unsigned int axis,unsigned short threshold) +{ +/* Sets the tap threshold in the dmp +Simultaneously sets secondary tap threshold to help correct the tap +direction for soft taps */ +int result; +/* DMP Algorithm */ +unsigned char data[2 ]; +int sampleDivider; +int scaledThreshold; +unsigned int dmpThreshold; +unsigned char sample_div; +const unsigned int accel_sens = (0x20000000 /0x00010000 ); + +if ((axis & ~(INV_TAP_AXIS_ALL )) || (threshold > (1 <<15 ))) +return -EINVAL ; + sample_div = st->sample_divider; + + sampleDivider = (1 + sample_div); +/* Scale factor corresponds linearly using +* 0 : 0 +* 25 : 0.0250 g/ms +* 50 : 0.0500 g/ms +* 100: 1.0000 g/ms +* 200: 2.0000 g/ms +* 400: 4.0000 g/ms +* 800: 8.0000 g/ms +*/ +/*multiply by 1000 to avoid floating point 1000/1000*/ + scaledThreshold = threshold; +/* Convert to per sample */ + scaledThreshold *= sampleDivider; + +/* Scale to DMP 16 bit value */ +if (accel_sens !=0 ) + dmpThreshold = (unsigned int )(scaledThreshold * accel_sens); +else +return -EINVAL ; + dmpThreshold = dmpThreshold /DMP_PRECISION ; + + data[0 ] = dmpThreshold >>8 ; + data[1 ] = dmpThreshold &0xFF ; + +/* MPL algorithm */ +if (axis &INV_TAP_AXIS_X ) { + result =mem_w_key (KEY_DMP_TAP_THR_X ,ARRAY_SIZE (data), data); +if (result) +return result; + +/*Also set additional threshold for correcting the direction +of taps that were very near the threshold. */ + data[0 ] = (dmpThreshold *3 /4 ) >>8 ; + data[1 ] = (dmpThreshold *3 /4 ) &0xFF ; + result =mem_w_key (KEY_D_1_36 ,ARRAY_SIZE (data), data); +if (result) +return result; + } +if (axis &INV_TAP_AXIS_Y ) { + result =mem_w_key (KEY_DMP_TAP_THR_Y ,2 , data); +if (result) +return result; + data[0 ] = (dmpThreshold *3 /4 ) >>8 ; + data[1 ] = (dmpThreshold *3 /4 ) &0xFF ; + + result =mem_w_key (KEY_D_1_40 ,ARRAY_SIZE (data), data); +if (result) +return result; + } +if (axis &INV_TAP_AXIS_Z ) { + result =mem_w_key (KEY_DMP_TAP_THR_Z ,ARRAY_SIZE (data), data); +if (result) +return result; + data[0 ] = (dmpThreshold *3 /4 ) >>8 ; + data[1 ] = (dmpThreshold *3 /4 ) &0xFF ; + + result =mem_w_key (KEY_D_1_44 ,ARRAY_SIZE (data), data); +if (result) +return result; + } +return 0 ; +} + +static int inv_set_tap_axes_dmp (struct inv_mpu_iio_s *st, +unsigned int axes) +{ +/* Sets a mask in the DMP that indicates what tap events +should result in an interrupt */ +unsigned char regs[4 ]; +unsigned char result; + +/* check if any spurious bit other the ones expected are set */ +if (axes & (~(INV_TAP_ALL_DIRECTIONS ))) +return -EINVAL ; + + regs[0 ] = (unsigned char )axes; + result =mem_w_key (KEY_D_1_72 ,1 , regs); + +return result; +} + +int inv_set_min_taps_dmp (struct inv_mpu_iio_s *st, +unsigned short min_taps) { +/*Indicates the minimum number of consecutive taps required +before the DMP will generate an interrupt */ +unsigned char regs[1 ]; +unsigned char result; +/* check if any spurious bit other the ones expected are set */ +if ((min_taps >DMP_MAX_MIN_TAPS ) || (min_taps <1 )) +return -EINVAL ; + regs[0 ] = (unsigned char )(min_taps-1 ); + result =mem_w_key (KEY_D_1_79 ,ARRAY_SIZE (regs), regs); + +return result; +} + +int inv_set_tap_time_dmp (struct inv_mpu_iio_s *st,unsigned short time) +{ +/* Determines how long after a tap the DMP requires before +another tap can be registered*/ +int result; +/* DMP Algorithm */ +unsigned short dmpTime; +unsigned char data[2 ]; +unsigned char sampleDivider; + + sampleDivider = st->sample_divider; + sampleDivider++; + +/* 60 ms minimum time added */ + dmpTime = ((time) / sampleDivider); + data[0 ] = dmpTime >>8 ; + data[1 ] = dmpTime &0xFF ; + + result =mem_w_key (KEY_DMP_TAPW_MIN ,ARRAY_SIZE (data), data); + +return result; +} + +static int inv_set_multiple_tap_time_dmp (struct inv_mpu_iio_s *st, +unsigned int time) +{ +/*Determines how close together consecutive taps must occur +to be considered double/triple taps*/ +int result; +/* DMP Algorithm */ +unsigned short dmpTime; +unsigned char data[2 ]; +unsigned char sampleDivider; + + sampleDivider = st->sample_divider; + sampleDivider++; + +/* 60 ms minimum time added */ + dmpTime = ((time) / sampleDivider); + data[0 ] = dmpTime >>8 ; + data[1 ] = dmpTime &0xFF ; + result =mem_w_key (KEY_D_1_218 ,ARRAY_SIZE (data), data); + +return result; +} + +int inv_q30_mult (int a,int b) +{ +long long temp; +int result; + temp = (long long )a * b; + result = (int )(temp >>DMP_MULTI_SHIFT ); + +return result; +} + +static unsigned short inv_row_2_scale (const signed char *row) +{ +unsigned short b; + +if (row[0 ] >0 ) + b =0 ; +else if (row[0 ] <0 ) + b =4 ; +else if (row[1 ] >0 ) + b =1 ; +else if (row[1 ] <0 ) + b =5 ; +else if (row[2 ] >0 ) + b =2 ; +else if (row[2 ] <0 ) + b =6 ; +else + b =7 ; + +return b; +} + +/** Converts an orientation matrix made up of 0,+1,and -1 to a scalar +* representation. +* @param[in] mtx Orientation matrix to convert to a scalar. +* @return Description of orientation matrix. The lowest 2 bits (0 and 1) +* represent the column the one is on for the +* first row, with the bit number 2 being the sign. The next 2 bits +* (3 and 4) represent +* the column the one is on for the second row with bit number 5 being +* the sign. +* The next 2 bits (6 and 7) represent the column the one is on for the +* third row with +* bit number 8 being the sign. In binary the identity matrix would therefor +* be: 010_001_000 or 0x88 in hex. +*/ +static unsigned short inv_orientation_matrix_to_scaler (const signed char *mtx) +{ + +unsigned short scalar; + scalar =inv_row_2_scale (mtx); + scalar |=inv_row_2_scale (mtx +3 ) <<3 ; + scalar |=inv_row_2_scale (mtx +6 ) <<6 ; + +return scalar; +} + +static int inv_gyro_dmp_cal (struct inv_mpu_iio_s *st) +{ +int inv_gyro_orient; +unsigned char regs[3 ]; +int result; + +unsigned char tmpD =DINA4C ; +unsigned char tmpE =DINACD ; +unsigned char tmpF =DINA6C ; + + inv_gyro_orient = +inv_orientation_matrix_to_scaler (st->plat_data.orientation); + +if ((inv_gyro_orient &3 ) ==0 ) + regs[0 ] = tmpD; +else if ((inv_gyro_orient &3 ) ==1 ) + regs[0 ] = tmpE; +else if ((inv_gyro_orient &3 ) ==2 ) + regs[0 ] = tmpF; +if ((inv_gyro_orient &0x18 ) ==0 ) + regs[1 ] = tmpD; +else if ((inv_gyro_orient &0x18 ) ==0x8 ) + regs[1 ] = tmpE; +else if ((inv_gyro_orient &0x18 ) ==0x10 ) + regs[1 ] = tmpF; +if ((inv_gyro_orient &0xc0 ) ==0 ) + regs[2 ] = tmpD; +else if ((inv_gyro_orient &0xc0 ) ==0x40 ) + regs[2 ] = tmpE; +else if ((inv_gyro_orient &0xc0 ) ==0x80 ) + regs[2 ] = tmpF; + + result =mem_w_key (KEY_FCFG_1 ,3 , regs); +if (result) +return result; + +if (inv_gyro_orient &4 ) + regs[0 ] =DINA36 |1 ; +else + regs[0 ] =DINA36 ; +if (inv_gyro_orient &0x20 ) + regs[1 ] =DINA56 |1 ; +else + regs[1 ] =DINA56 ; +if (inv_gyro_orient &0x100 ) + regs[2 ] =DINA76 |1 ; +else + regs[2 ] =DINA76 ; + + result =mem_w_key (KEY_FCFG_3 ,ARRAY_SIZE (regs), regs); + +return result; +} + +static int inv_accel_dmp_cal (struct inv_mpu_iio_s *st) +{ +int inv_accel_orient; +int result; +unsigned char regs[3 ]; +const unsigned char tmp[3 ] = {DINA0C ,DINAC9 ,DINA2C }; + inv_accel_orient = +inv_orientation_matrix_to_scaler (st->plat_data.orientation); + + regs[0 ] = tmp[inv_accel_orient &3 ]; + regs[1 ] = tmp[(inv_accel_orient >>3 ) &3 ]; + regs[2 ] = tmp[(inv_accel_orient >>6 ) &3 ]; + result =mem_w_key (KEY_FCFG_2 ,3 , regs); +if (result) +return result; + + regs[0 ] =DINA26 ; + regs[1 ] =DINA46 ; + regs[2 ] =DINA66 ; +if (inv_accel_orient &4 ) + regs[0 ] |=1 ; +if (inv_accel_orient &0x20 ) + regs[1 ] |=1 ; +if (inv_accel_orient &0x100 ) + regs[2 ] |=1 ; + result =mem_w_key (KEY_FCFG_7 ,ARRAY_SIZE (regs), regs); + +return result; +} + +static int inv_set_gyro_sf_dmp (struct inv_mpu_iio_s *st) +{ +/*The gyro threshold, in dps, above which taps will be rejected*/ +int result, out; +/* DMP Algorithm */ +unsigned char sampleDivider; +unsigned char *regs; +unsigned int gyro_sf; +const unsigned int gyro_sens =0x03e80000 ; + + sampleDivider = st->sample_divider; + gyro_sf =inv_q30_mult (gyro_sens, + (int )(DMP_TAP_SCALE * (sampleDivider +1 ))); + + out =cpu_to_be32p (&gyro_sf); + regs = (unsigned char *)&out; + result =mem_w_key (KEY_D_0_104 ,sizeof (out), regs); + +return result; +} + +static int inv_set_shake_reject_thresh_dmp (struct inv_mpu_iio_s *st, +int thresh) +{/*THIS FUNCTION FAILS MEM_W*/ +/*The gyro threshold, in dps, above which taps will be rejected */ +int result, out; +/* DMP Algorithm */ +unsigned char sampleDivider; +int thresh_scaled; +unsigned char *regs; +unsigned int gyro_sf; +const unsigned int gyro_sens =0x03e80000 ; + sampleDivider = st->sample_divider; + gyro_sf =inv_q30_mult (gyro_sens, (int )(DMP_TAP_SCALE * + (sampleDivider +1 ))); +/* We're in units of DPS, convert it back to chip units*/ +/*split the operation to aviod overflow of integer*/ + thresh_scaled = gyro_sens / (1L <<16 ); + thresh_scaled = thresh_scaled / thresh; + thresh_scaled = gyro_sf / thresh_scaled; + out =cpu_to_be32p (&thresh_scaled); + regs = (unsigned char *)&out; + + result =mem_w_key (KEY_D_1_92 ,sizeof (out), regs); +return result; +} + +static int inv_set_shake_reject_time_dmp (struct inv_mpu_iio_s *st, +unsigned int time) +{ +/* How long a gyro axis must remain above its threshold +before taps are rejected */ +int result; +/* DMP Algorithm */ +unsigned short dmpTime; +unsigned char data[2 ]; +unsigned char sampleDivider; + + sampleDivider = st->sample_divider; + sampleDivider++; + +/* 60 ms minimum time added */ + dmpTime = ((time) / sampleDivider); + data[0 ] = dmpTime >>8 ; + data[1 ] = dmpTime &0xFF ; + + result =mem_w_key (KEY_D_1_88 ,ARRAY_SIZE (data), data); +return result; +} + +static int inv_set_shake_reject_timeout_dmp (struct inv_mpu_iio_s *st, +unsigned int time) +{ +/*How long the gyros must remain below their threshold, +after taps have been rejected, before taps can be detected again*/ +int result; +/* DMP Algorithm */ +unsigned short dmpTime; +unsigned char data[2 ]; +unsigned char sampleDivider; + + sampleDivider = st->sample_divider; + sampleDivider++; + +/* 60 ms minimum time added */ + dmpTime = ((time) / sampleDivider); + data[0 ] = dmpTime >>8 ; + data[1 ] = dmpTime &0xFF ; + + result =mem_w_key (KEY_D_1_90 ,ARRAY_SIZE (data), data); +return result; +} + +int inv_set_interrupt_on_gesture_event (struct inv_mpu_iio_s *st,bool on) +{ +unsigned char result; +const unsigned char regs_on[] = {DINADA ,DINADA ,DINAB1 ,DINAB9 , +DINAF3 ,DINA8B ,DINAA3 ,DINA91 , +DINAB6 ,DINADA ,DINAB4 ,DINADA }; +const unsigned char regs_off[] = {0xd8 ,0xd8 ,0xb1 ,0xb9 ,0xf3 ,0x8b , +0xa3 ,0x91 ,0xb6 ,0x09 ,0xb4 ,0xd9 }; +/*For some reason DINAC4 is defined as 0xb8, +but DINBC4 is not defined.*/ +const unsigned char regs_end[] = {DINAFE ,DINAF2 ,DINAAB ,0xc4 , +DINAAA ,DINAF1 ,DINADF ,DINADF }; +if (on) +/*Sets the DMP to send an interrupt and put a FIFO packet +in the FIFO if and only if a tap/orientation event +just occurred*/ + result =mem_w_key (KEY_CFG_FIFO_ON_EVENT ,ARRAY_SIZE (regs_on), + regs_on); +else +/*Sets the DMP to send an interrupt and put a FIFO packet +in the FIFO at the rate specified by the FIFO div. +see inv_set_fifo_div in hw_setup.c to set the FIFO div.*/ + result =mem_w_key (KEY_CFG_FIFO_ON_EVENT ,ARRAY_SIZE (regs_off), + regs_off); +if (result) +return result; + + result =mem_w_key (KEY_CFG_6 ,ARRAY_SIZE (regs_end), regs_end); +return result; +} + +/** +* inv_enable_tap_dmp() - calling this function will enable/disable tap function. +*/ +int inv_enable_tap_dmp (struct inv_mpu_iio_s *st,bool on) +{ +int result; + result =inv_set_tap_interrupt_dmp (st, on); +if (result) +return result; +if (on) { + result =inv_set_tap_threshold_dmp (st,INV_TAP_AXIS_X , + st->tap.thresh); +if (result) +return result; + result =inv_set_tap_threshold_dmp (st,INV_TAP_AXIS_Y , + st->tap.thresh); +if (result) +return result; + result =inv_set_tap_threshold_dmp (st,INV_TAP_AXIS_Z , + st->tap.thresh); +if (result) +return result; + } + + result =inv_set_tap_axes_dmp (st,INV_TAP_ALL_DIRECTIONS ); +if (result) +return result; + result =inv_set_min_taps_dmp (st, st->tap.min_count); +if (result) +return result; + + result =inv_set_tap_time_dmp (st, st->tap.time); +if (result) +return result; + + result =inv_set_multiple_tap_time_dmp (st,DMP_MULTI_TAP_TIME ); +if (result) +return result; + + result =inv_set_gyro_sf_dmp (st); +if (result) +return result; + + result =inv_set_shake_reject_thresh_dmp (st,DMP_SHAKE_REJECT_THRESH ); +if (result) +return result; + + result =inv_set_shake_reject_time_dmp (st,DMP_SHAKE_REJECT_TIME ); +if (result) +return result; + + result =inv_set_shake_reject_timeout_dmp (st, +DMP_SHAKE_REJECT_TIMEOUT ); +return result; +} + +static int inv_set_orientation_dmp (struct inv_mpu_iio_s *st, +int orientation) +{ +/*Set a mask in the DMP determining what orientations +will trigger interrupts*/ +unsigned char regs[4 ]; +unsigned char result; + +/* check if any spurious bit other the ones expected are set */ +if (orientation & (~(INV_ORIENTATION_ALL |INV_ORIENTATION_FLIP ))) +return -EINVAL ; + + regs[0 ] = (unsigned char )orientation; + result =mem_w_key (KEY_D_1_74 ,1 , regs); +return result; +} + +static int inv_set_orientation_thresh_dmp (struct inv_mpu_iio_s *st, +int angle) +{ +/*Set an angle threshold in the DMP determining +when orientations change*/ +unsigned char *regs; +unsigned char result; +unsigned int out; +unsigned int d; +const unsigned int threshold[] = {138952416 ,268435455 ,379625062 , +464943848 ,518577479 ,536870912 }; +/* The real calculation is +* threshold = (long)((1 << 29) * sin((angle * M_PI) / 180.)); +* Here we have to use table lookup*/ + d = angle /DMP_ANGLE_SCALE ; + d -=1 ; +if (d >=ARRAY_SIZE (threshold)) +return -EPERM ; + out =cpu_to_be32p (&threshold[d]); + regs = (unsigned char *)&out; + + result =mem_w_key (KEY_D_1_232 ,sizeof (out), regs); +return result; +} + +static int inv_set_orientation_time_dmp (struct inv_mpu_iio_s *st, +unsigned int time) +{ +/*Determines the stability time required before a +new orientation can be adopted */ +unsigned short dmpTime; +unsigned char data[2 ]; +unsigned char sampleDivider; +unsigned char result; +/* First check if we are allowed to call this function here */ + sampleDivider = st->sample_divider; + sampleDivider++; +/* 60 ms minimum time added */ + dmpTime = ((time) / sampleDivider); + data[0 ] = dmpTime >>8 ; + data[1 ] = dmpTime &0xFF ; + result =mem_w_key (KEY_D_1_250 ,2 , data); + +return result; +} + +/** +* inv_enable_orientation_dmp() - calling this function will +* enable/disable orientation function. +*/ +int inv_enable_orientation_dmp (struct inv_mpu_iio_s *st,bool on) +{ +int result; + result =inv_set_orientation_interrupt_dmp (st, on); +if (result) +return result; + result =inv_set_orientation_dmp (st,0x40 |INV_ORIENTATION_ALL ); +if (result) +return result; + result =inv_set_gyro_sf_dmp (st); +if (result) +return result; + result =inv_set_orientation_thresh_dmp (st,DMP_ORIENTATION_ANGLE ); +if (result) +return result; + result =inv_set_orientation_time_dmp (st,DMP_ORIENTATION_TIME ); +return result; +} + +static int inv_send_sensor_data (struct inv_mpu_iio_s *st, +unsigned short elements) +{ +int result; +unsigned char regs[] = {DINAA0 +3 ,DINAA0 +3 ,DINAA0 +3 , +DINAA0 +3 ,DINAA0 +3 ,DINAA0 +3 , +DINAA0 +3 ,DINAA0 +3 ,DINAA0 +3 , +DINAA0 +3 }; + +if (elements &INV_ELEMENT_1 ) + regs[0 ] =DINACA ; +if (elements &INV_ELEMENT_2 ) + regs[4 ] =DINBC4 ; +if (elements &INV_ELEMENT_3 ) + regs[5 ] =DINACC ; +if (elements &INV_ELEMENT_4 ) + regs[6 ] =DINBC6 ; +if ((elements &INV_ELEMENT_5 ) || (elements &INV_ELEMENT_6 ) || + (elements &INV_ELEMENT_7 )) { + regs[1 ] =DINBC0 ; + regs[2 ] =DINAC8 ; + regs[3 ] =DINBC2 ; + } + result =mem_w_key (KEY_CFG_15 ,ARRAY_SIZE (regs), regs); +return result; +} + +static int inv_send_interrupt_word (struct inv_mpu_iio_s *st) +{ +const unsigned char regs[] = {DINA20 }; +unsigned char result; + + result =mem_w_key (KEY_CFG_27 ,ARRAY_SIZE (regs), regs); +return result; +} + +/** +* inv_dmp_firmware_write() - calling this function will load the firmware. +* This is the write function of file "dmp_firmware". +*/ +ssize_t inv_dmp_firmware_write (struct file *fp,struct kobject *kobj, +struct bin_attribute *attr, +char *buf,loff_t pos,size_t size) +{ +unsigned char *firmware; +int result; +struct inv_reg_map_s *reg; +struct iio_dev *indio_dev; +struct inv_mpu_iio_s *st; + + indio_dev = dev_get_drvdata(container_of(kobj, struct device, kobj)); + st =iio_priv (indio_dev); + +if (st->chip_config.is_asleep || st->chip_config.firmware_loaded) +return -EINVAL ; + + reg = &st->reg; +if (DMP_IMAGE_SIZE != size) { +pr_err ("wrong DMP image size\n" ); +return -EINVAL ; + } + + firmware =kmalloc (size,GFP_KERNEL ); +if (!firmware) +return -ENOMEM ; + +memcpy (firmware, buf, size); + result =crc32 (CRC_FIRMWARE_SEED , firmware, size); +if (DMP_IMAGE_CRC_VALUE != result) { + result = -EINVAL ; +pr_err ("firmware CRC error\n" ); + goto firmware_write_fail; + } + + result =inv_load_firmware (st, firmware, size); +if (result) + goto firmware_write_fail; + + result =inv_verify_firmware (st, firmware, size); +if (result) + goto firmware_write_fail; + + result =inv_i2c_single_write (st, reg->prgm_strt_addrh, + st->chip_config.prog_start_addr >>8 ); +if (result) + goto firmware_write_fail; + result =inv_i2c_single_write (st, reg->prgm_strt_addrh +1 , + st->chip_config.prog_start_addr &0xff ); +if (result) + goto firmware_write_fail; + + result =inv_verify_firmware (st, firmware, size); +if (result) + goto firmware_write_fail; + result =inv_set_fifo_rate (st,DMP_DEFAULT_FIFO_RATE ); +if (result) + goto firmware_write_fail; + result =inv_send_sensor_data (st,INV_GYRO_ACC_MASK ); +if (result) + goto firmware_write_fail; + result =inv_send_interrupt_word (st); +if (result) + goto firmware_write_fail; + result =inv_gyro_dmp_cal (st); +if (result) + goto firmware_write_fail; + result =inv_accel_dmp_cal (st); +if (result) + goto firmware_write_fail; + st->chip_config.firmware_loaded =1 ; + result = size; +firmware_write_fail: +kfree (firmware); + +return result; +} + +ssize_t inv_dmp_firmware_read (struct file *filp, +struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buf,loff_t off,size_t count) +{ +int bank, write_size, size, data, result; +unsigned short memaddr; +struct iio_dev *indio_dev; +struct inv_mpu_iio_s *st; + size = count; + + indio_dev = dev_get_drvdata(container_of(kobj, struct device, kobj)); + st =iio_priv (indio_dev); + data =0 ; +for (bank =0 ; size >0 ; bank++, size -= write_size, + data += write_size) { +if (size >MPU_MEM_BANK_SIZE ) + write_size =MPU_MEM_BANK_SIZE ; +else + write_size = size; + + memaddr = ((bank <<8 ) |0x00 ); + result =mpu_memory_read (st->sl_handle, + st->i2c_addr, memaddr, write_size, &buf[data]); +if (result) +return result; + } + +return 0 ; +} +/** +* @} +*/ +
Signature · 1 line
1.7.0.4