aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/mpu3050/mpu-i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/misc/mpu3050/mpu-i2c.c')
-rw-r--r--drivers/misc/mpu3050/mpu-i2c.c196
1 files changed, 196 insertions, 0 deletions
diff --git a/drivers/misc/mpu3050/mpu-i2c.c b/drivers/misc/mpu3050/mpu-i2c.c
new file mode 100644
index 00000000000..b1298d313ab
--- /dev/null
+++ b/drivers/misc/mpu3050/mpu-i2c.c
@@ -0,0 +1,196 @@
1/*
2 $License:
3 Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 $
18 */
19
20/**
21 * @defgroup
22 * @brief
23 *
24 * @{
25 * @file mpu-i2c.c
26 * @brief
27 *
28 */
29
30#include <linux/i2c.h>
31#include "mpu.h"
32
33int sensor_i2c_write(struct i2c_adapter *i2c_adap,
34 unsigned char address,
35 unsigned int len, unsigned char const *data)
36{
37 struct i2c_msg msgs[1];
38 int res;
39
40 if (NULL == data || NULL == i2c_adap)
41 return -EINVAL;
42
43 msgs[0].addr = address;
44 msgs[0].flags = 0; /* write */
45 msgs[0].buf = (unsigned char *) data;
46 msgs[0].len = len;
47
48 res = i2c_transfer(i2c_adap, msgs, 1);
49 if (res < 1)
50 return res;
51 else
52 return 0;
53}
54
55int sensor_i2c_write_register(struct i2c_adapter *i2c_adap,
56 unsigned char address,
57 unsigned char reg, unsigned char value)
58{
59 unsigned char data[2];
60
61 data[0] = reg;
62 data[1] = value;
63 return sensor_i2c_write(i2c_adap, address, 2, data);
64}
65
66int sensor_i2c_read(struct i2c_adapter *i2c_adap,
67 unsigned char address,
68 unsigned char reg,
69 unsigned int len, unsigned char *data)
70{
71 struct i2c_msg msgs[2];
72 int res;
73
74 if (NULL == data || NULL == i2c_adap)
75 return -EINVAL;
76
77 msgs[0].addr = address;
78 msgs[0].flags = 0; /* write */
79 msgs[0].buf = &reg;
80 msgs[0].len = 1;
81
82 msgs[1].addr = address;
83 msgs[1].flags = I2C_M_RD;
84 msgs[1].buf = data;
85 msgs[1].len = len;
86
87 res = i2c_transfer(i2c_adap, msgs, 2);
88 if (res < 2)
89 return res;
90 else
91 return 0;
92}
93
94int mpu_memory_read(struct i2c_adapter *i2c_adap,
95 unsigned char mpu_addr,
96 unsigned short mem_addr,
97 unsigned int len, unsigned char *data)
98{
99 unsigned char bank[2];
100 unsigned char addr[2];
101 unsigned char buf;
102
103 struct i2c_msg msgs[4];
104 int ret;
105
106 if (NULL == data || NULL == i2c_adap)
107 return -EINVAL;
108
109 bank[0] = MPUREG_BANK_SEL;
110 bank[1] = mem_addr >> 8;
111
112 addr[0] = MPUREG_MEM_START_ADDR;
113 addr[1] = mem_addr & 0xFF;
114
115 buf = MPUREG_MEM_R_W;
116
117 /* Write Message */
118 msgs[0].addr = mpu_addr;
119 msgs[0].flags = 0;
120 msgs[0].buf = bank;
121 msgs[0].len = sizeof(bank);
122
123 msgs[1].addr = mpu_addr;
124 msgs[1].flags = 0;
125 msgs[1].buf = addr;
126 msgs[1].len = sizeof(addr);
127
128 msgs[2].addr = mpu_addr;
129 msgs[2].flags = 0;
130 msgs[2].buf = &buf;
131 msgs[2].len = 1;
132
133 msgs[3].addr = mpu_addr;
134 msgs[3].flags = I2C_M_RD;
135 msgs[3].buf = data;
136 msgs[3].len = len;
137
138 ret = i2c_transfer(i2c_adap, msgs, 4);
139 if (ret != 4)
140 return ret;
141 else
142 return 0;
143}
144
145int mpu_memory_write(struct i2c_adapter *i2c_adap,
146 unsigned char mpu_addr,
147 unsigned short mem_addr,
148 unsigned int len, unsigned char const *data)
149{
150 unsigned char bank[2];
151 unsigned char addr[2];
152 unsigned char buf[513];
153
154 struct i2c_msg msgs[3];
155 int ret;
156
157 if (NULL == data || NULL == i2c_adap)
158 return -EINVAL;
159 if (len >= (sizeof(buf) - 1))
160 return -ENOMEM;
161
162 bank[0] = MPUREG_BANK_SEL;
163 bank[1] = mem_addr >> 8;
164
165 addr[0] = MPUREG_MEM_START_ADDR;
166 addr[1] = mem_addr & 0xFF;
167
168 buf[0] = MPUREG_MEM_R_W;
169 memcpy(buf + 1, data, len);
170
171 /* Write Message */
172 msgs[0].addr = mpu_addr;
173 msgs[0].flags = 0;
174 msgs[0].buf = bank;
175 msgs[0].len = sizeof(bank);
176
177 msgs[1].addr = mpu_addr;
178 msgs[1].flags = 0;
179 msgs[1].buf = addr;
180 msgs[1].len = sizeof(addr);
181
182 msgs[2].addr = mpu_addr;
183 msgs[2].flags = 0;
184 msgs[2].buf = (unsigned char *) buf;
185 msgs[2].len = len + 1;
186
187 ret = i2c_transfer(i2c_adap, msgs, 3);
188 if (ret != 3)
189 return ret;
190 else
191 return 0;
192}
193
194/**
195 * @}
196 */