mpu-i2c.c 3.9 KB

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