summaryrefslogtreecommitdiff
path: root/drivers/input/misc/mpu/inv_mpu3050.c
blob: c9882d7c7e6603825fbf631fa6000d4cee435c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (C) 2012 Invensense, Inc.
* Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
*
* 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.
*
*/

/**
 *  @addtogroup  DRIVERS
 *  @brief       Hardware drivers.
 *
 *  @{
 *      @file    inv_mpu3050.c
 *      @brief   A sysfs device driver for Invensense devices
 *      @details This file is part of inv_gyro driver code
 */

#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/spinlock.h>

#include "inv_gyro.h"


void inv_setup_reg_mpu3050(struct inv_reg_map_s *reg)
{
	reg->who_am_i		= 0x00;
	reg->fifo_en		= 0x12;
	reg->sample_rate_div	= 0x15;
	reg->lpf		= 0x16;
	reg->fifo_count_h	= 0x3A;
	reg->fifo_r_w		= 0x3C;
	reg->user_ctrl		= 0x3D;
	reg->pwr_mgmt_1		= 0x3E;
	reg->raw_gyro		= 0x1D;
	reg->raw_accl		= 0x23;
	reg->temperature	= 0x1B;
	reg->int_enable		= 0x17;
	reg->int_status		= 0x1A;

	reg->accl_fifo_en	= BITS_3050_ACCL_OUT;
	reg->fifo_reset		= BIT_3050_FIFO_RST;
	reg->i2c_mst_reset	= BIT_3050_AUX_IF_RST;
	reg->cycle		= 0;
	reg->temp_dis		= 0;
}

/**
 *  inv_init_config_mpu3050() - Initialize hardware, disable FIFO.
 *  @st:	Device driver instance.
 */
int inv_init_config_mpu3050(struct inv_gyro_state_s *st)
{
	u8 data;
	int result;

	st->chip_config.fifo_thr = FIFO_THRESHOLD;

	/*reading AUX VDDIO register */
	result = inv_i2c_read(st, REG_3050_AUX_VDDIO, 1, &data);
	if (result)
		return result;

	data &= ~BIT_3050_VDDIO;
	data |= (st->plat_data.level_shifter << 2);
	result = inv_i2c_single_write(st, REG_3050_AUX_VDDIO, data);
	if (result)
		return result;

	if (SECONDARY_SLAVE_TYPE_ACCEL == st->plat_data.sec_slave_type) {
		if (st->plat_data.sec_slave_id == ACCEL_ID_KXTF9)
			inv_register_kxtf9_slave(st);
		if (st->mpu_slave != NULL) {
			result = st->mpu_slave->setup(st);
			if (result)
				return result;
		}
	}
	return 0;
}

int set_3050_bypass(struct inv_gyro_state_s *inf, int enable)
{
	struct inv_reg_map_s *reg;
	u8 user_ctrl;
	int err;
	int err_t = 0;

	reg = inf->reg;
	if (((inf->hw.user_ctrl & BIT_3050_AUX_IF_EN) == 0) && enable)
		return 0;

	if ((inf->hw.user_ctrl & BIT_3050_AUX_IF_EN) && (enable == 0))
		return 0;

	user_ctrl = inf->hw.user_ctrl;
	user_ctrl &= ~BIT_3050_AUX_IF_EN;
	if (!enable) {
		user_ctrl |= BIT_3050_AUX_IF_EN;
		err_t = inv_i2c_single_write(inf, reg->user_ctrl, user_ctrl);
		if (!err_t) {
			inf->hw.user_ctrl = user_ctrl;
			inf->aux.en3050 = true;
		}
	} else {
		inf->aux.en3050 = false;
		/* Coming out of I2C is tricky due to several erratta.
		* Do not modify this algorithm
		* 1) wait for the right time and send the command to change
		* the aux i2c slave address to an invalid address that will
		* get nack'ed
		*
		* 0x00 is broadcast.  0x7F is unlikely to be used by any aux.
		*/
		err_t = inv_i2c_single_write(inf, REG_3050_SLAVE_ADDR, 0x7F);
		/*
		* 2) wait enough time for a nack to occur, then go into
		*    bypass mode:
		*/
		mdelay(2);
		err = inv_i2c_single_write(inf, reg->user_ctrl, user_ctrl);
		if (!err)
			inf->hw.user_ctrl = user_ctrl;
		else
			err_t |= err;
		/*
		* 3) wait for up to one MPU cycle then restore the slave
		*    address
		*/
		mdelay(20);
		err_t |= inv_i2c_single_write(inf, REG_3050_SLAVE_REG,
					    inf->plat_data.secondary_read_reg);
		err_t |= inv_i2c_single_write(inf, REG_3050_SLAVE_ADDR,
					    inf->plat_data.secondary_i2c_addr);
		err = inv_i2c_single_write(inf, reg->user_ctrl,
					   (user_ctrl | BIT_3050_AUX_IF_RST));
		if (!err)
			inf->hw.user_ctrl = user_ctrl;
		else
			err_t |= err;
		mdelay(2);
	}
	return err_t;
}
/**
 *  @}
 */