3 Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
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.
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.
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/>.
21 #include <linux/i2c.h>
23 #include "mpu6050b1.h"
25 #define MPU_I2C_RATE 100*1000
26 static int inv_i2c_write(struct i2c_adapter *i2c_adap,
27 unsigned char address,
28 unsigned int len, unsigned char const *data)
30 struct i2c_msg msgs[1];
33 if (!data || !i2c_adap) {
34 LOG_RESULT_LOCATION(-EINVAL);
38 msgs[0].addr = address;
39 msgs[0].flags = 0; /* write */
40 msgs[0].buf = (unsigned char *)data;
42 msgs[0].scl_rate = MPU_I2C_RATE;
44 res = i2c_transfer(i2c_adap, msgs, 1);
48 LOG_RESULT_LOCATION(res);
54 static int inv_i2c_write_register(struct i2c_adapter *i2c_adap,
55 unsigned char address,
56 unsigned char reg, unsigned char value)
58 unsigned char data[2];
62 return inv_i2c_write(i2c_adap, address, 2, data);
65 static int inv_i2c_read(struct i2c_adapter *i2c_adap,
66 unsigned char address, unsigned char reg,
67 unsigned int len, unsigned char *data)
69 struct i2c_msg msgs[2];
72 if (!data || !i2c_adap) {
73 LOG_RESULT_LOCATION(-EINVAL);
77 msgs[0].addr = address;
78 msgs[0].flags = 0; /* write */
81 msgs[0].scl_rate = MPU_I2C_RATE;
83 msgs[1].addr = address;
84 msgs[1].flags = I2C_M_RD;
87 msgs[1].scl_rate = MPU_I2C_RATE;
89 res = i2c_transfer(i2c_adap, msgs, 2);
93 LOG_RESULT_LOCATION(res);
99 static int mpu_memory_read(struct i2c_adapter *i2c_adap,
100 unsigned char mpu_addr,
101 unsigned short mem_addr,
102 unsigned int len, unsigned char *data)
104 unsigned char bank[2];
105 unsigned char addr[2];
107 struct i2c_msg msgs[2];
110 if (!data || !i2c_adap) {
111 LOG_RESULT_LOCATION(-EINVAL);
115 bank[0] = MPUREG_BANK_SEL;
116 bank[1] = mem_addr >> 8;
118 addr[0] = MPUREG_MEM_START_ADDR;
119 addr[1] = mem_addr & 0xFF;
121 buf = MPUREG_MEM_R_W;
124 msgs[0].addr = mpu_addr;
127 msgs[0].len = sizeof(bank);
128 msgs[0].scl_rate = MPU_I2C_RATE;
130 res = i2c_transfer(i2c_adap, msgs, 1);
134 msgs[0].addr = mpu_addr;
137 msgs[0].len = sizeof(addr);
138 msgs[0].scl_rate = MPU_I2C_RATE;
140 res = i2c_transfer(i2c_adap, msgs, 1);
144 msgs[0].addr = mpu_addr;
148 msgs[0].scl_rate = MPU_I2C_RATE;
150 msgs[1].addr = mpu_addr;
151 msgs[1].flags = I2C_M_RD;
154 msgs[1].scl_rate = MPU_I2C_RATE;
156 res = i2c_transfer(i2c_adap, msgs, 2);
165 static int mpu_memory_write(struct i2c_adapter *i2c_adap,
166 unsigned char mpu_addr,
167 unsigned short mem_addr,
168 unsigned int len, unsigned char const *data)
170 unsigned char bank[2];
171 unsigned char addr[2];
172 unsigned char buf[513];
174 struct i2c_msg msgs[2];
177 if (!data || !i2c_adap) {
178 LOG_RESULT_LOCATION(-EINVAL);
181 if (len >= (sizeof(buf) - 1)) {
182 LOG_RESULT_LOCATION(-ENOMEM);
186 bank[0] = MPUREG_BANK_SEL;
187 bank[1] = mem_addr >> 8;
189 addr[0] = MPUREG_MEM_START_ADDR;
190 addr[1] = mem_addr & 0xFF;
192 buf[0] = MPUREG_MEM_R_W;
193 memcpy(buf + 1, data, len);
196 msgs[0].addr = mpu_addr;
199 msgs[0].len = sizeof(bank);
200 msgs[0].scl_rate = MPU_I2C_RATE;
202 res = i2c_transfer(i2c_adap, msgs, 1);
206 msgs[0].addr = mpu_addr;
209 msgs[0].scl_rate = MPU_I2C_RATE;
211 res = i2c_transfer(i2c_adap, msgs, 1);
215 msgs[0].addr = mpu_addr;
217 msgs[0].buf = (unsigned char *)buf;
218 msgs[0].len = len + 1;
219 msgs[0].scl_rate = MPU_I2C_RATE;
221 res = i2c_transfer(i2c_adap, msgs, 1);
231 int inv_serial_single_write(
233 unsigned char slave_addr,
234 unsigned char register_addr,
237 return inv_i2c_write_register((struct i2c_adapter *)sl_handle,
238 slave_addr, register_addr, data);
240 EXPORT_SYMBOL(inv_serial_single_write);
242 int inv_serial_write(
244 unsigned char slave_addr,
245 unsigned short length,
246 unsigned char const *data)
249 const unsigned short data_length = length - 1;
250 const unsigned char start_reg_addr = data[0];
251 unsigned char i2c_write[SERIAL_MAX_TRANSFER_SIZE + 1];
252 unsigned short bytes_written = 0;
254 while (bytes_written < data_length) {
255 unsigned short this_len = min(SERIAL_MAX_TRANSFER_SIZE,
256 data_length - bytes_written);
257 if (bytes_written == 0) {
258 result = inv_i2c_write((struct i2c_adapter *)
259 sl_handle, slave_addr,
262 /* manually increment register addr between chunks */
263 i2c_write[0] = start_reg_addr + bytes_written;
264 memcpy(&i2c_write[1], &data[1 + bytes_written],
266 result = inv_i2c_write((struct i2c_adapter *)
267 sl_handle, slave_addr,
268 1 + this_len, i2c_write);
271 LOG_RESULT_LOCATION(result);
274 bytes_written += this_len;
278 EXPORT_SYMBOL(inv_serial_write);
282 unsigned char slave_addr,
283 unsigned char register_addr,
284 unsigned short length,
288 unsigned short bytes_read = 0;
290 if ((slave_addr & 0x7E) == DEFAULT_MPU_SLAVEADDR
291 && (register_addr == MPUREG_FIFO_R_W ||
292 register_addr == MPUREG_MEM_R_W)) {
293 LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
294 return INV_ERROR_INVALID_PARAMETER;
297 while (bytes_read < length) {
298 unsigned short this_len =
299 min(SERIAL_MAX_TRANSFER_SIZE, length - bytes_read);
300 result = inv_i2c_read((struct i2c_adapter *)sl_handle,
301 slave_addr, register_addr + bytes_read,
302 this_len, &data[bytes_read]);
304 LOG_RESULT_LOCATION(result);
307 bytes_read += this_len;
311 EXPORT_SYMBOL(inv_serial_read);
313 int inv_serial_write_mem(
315 unsigned char slave_addr,
316 unsigned short mem_addr,
317 unsigned short length,
318 unsigned char const *data)
321 unsigned short bytes_written = 0;
323 if ((mem_addr & 0xFF) + length > MPU_MEM_BANK_SIZE) {
324 pr_err("memory read length (%d B) extends beyond its"
325 " limits (%d) if started at location %d\n", length,
326 MPU_MEM_BANK_SIZE, mem_addr & 0xFF);
327 return INV_ERROR_INVALID_PARAMETER;
329 while (bytes_written < length) {
330 unsigned short this_len =
331 min(SERIAL_MAX_TRANSFER_SIZE, length - bytes_written);
332 result = mpu_memory_write((struct i2c_adapter *)sl_handle,
333 slave_addr, mem_addr + bytes_written,
334 this_len, &data[bytes_written]);
336 LOG_RESULT_LOCATION(result);
339 bytes_written += this_len;
343 EXPORT_SYMBOL(inv_serial_write_mem);
345 int inv_serial_read_mem(
347 unsigned char slave_addr,
348 unsigned short mem_addr,
349 unsigned short length,
353 unsigned short bytes_read = 0;
355 if ((mem_addr & 0xFF) + length > MPU_MEM_BANK_SIZE) {
357 ("memory read length (%d B) extends beyond its limits (%d) "
358 "if started at location %d\n", length,
359 MPU_MEM_BANK_SIZE, mem_addr & 0xFF);
360 return INV_ERROR_INVALID_PARAMETER;
362 while (bytes_read < length) {
363 unsigned short this_len =
364 min(SERIAL_MAX_TRANSFER_SIZE, length - bytes_read);
366 mpu_memory_read((struct i2c_adapter *)sl_handle,
367 slave_addr, mem_addr + bytes_read,
368 this_len, &data[bytes_read]);
370 LOG_RESULT_LOCATION(result);
373 bytes_read += this_len;
377 EXPORT_SYMBOL(inv_serial_read_mem);
379 int inv_serial_write_fifo(
381 unsigned char slave_addr,
382 unsigned short length,
383 unsigned char const *data)
386 unsigned char i2c_write[SERIAL_MAX_TRANSFER_SIZE + 1];
387 unsigned short bytes_written = 0;
389 if (length > FIFO_HW_SIZE) {
391 "maximum fifo write length is %d\n", FIFO_HW_SIZE);
392 return INV_ERROR_INVALID_PARAMETER;
394 while (bytes_written < length) {
395 unsigned short this_len =
396 min(SERIAL_MAX_TRANSFER_SIZE, length - bytes_written);
397 i2c_write[0] = MPUREG_FIFO_R_W;
398 memcpy(&i2c_write[1], &data[bytes_written], this_len);
399 result = inv_i2c_write((struct i2c_adapter *)sl_handle,
400 slave_addr, this_len + 1, i2c_write);
402 LOG_RESULT_LOCATION(result);
405 bytes_written += this_len;
409 EXPORT_SYMBOL(inv_serial_write_fifo);
411 int inv_serial_read_fifo(
413 unsigned char slave_addr,
414 unsigned short length,
418 unsigned short bytes_read = 0;
420 if (length > FIFO_HW_SIZE) {
422 "maximum fifo read length is %d\n", FIFO_HW_SIZE);
423 return INV_ERROR_INVALID_PARAMETER;
425 while (bytes_read < length) {
426 unsigned short this_len =
427 min(SERIAL_MAX_TRANSFER_SIZE, length - bytes_read);
428 result = inv_i2c_read((struct i2c_adapter *)sl_handle,
429 slave_addr, MPUREG_FIFO_R_W, this_len,
432 LOG_RESULT_LOCATION(result);
435 bytes_read += this_len;
440 EXPORT_SYMBOL(inv_serial_read_fifo);