mlsl-kernel.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #include "mlsl.h"
  17. #include "mpu-i2c.h"
  18. /* ------------ */
  19. /* - Defines. - */
  20. /* ------------ */
  21. /* ---------------------- */
  22. /* - Types definitions. - */
  23. /* ---------------------- */
  24. /* --------------------- */
  25. /* - Function p-types. - */
  26. /* --------------------- */
  27. /**
  28. * @brief used to open the I2C or SPI serial port.
  29. * This port is used to send and receive data to the MPU device.
  30. * @param portNum
  31. * The COM port number associated with the device in use.
  32. * @return ML_SUCCESS if successful, a non-zero error code otherwise.
  33. */
  34. tMLError MLSLSerialOpen(char const *port, void **sl_handle)
  35. {
  36. return ML_SUCCESS;
  37. }
  38. /**
  39. * @brief used to reset any buffering the driver may be doing
  40. * @return ML_SUCCESS if successful, a non-zero error code otherwise.
  41. */
  42. tMLError MLSLSerialReset(void *sl_handle)
  43. {
  44. return ML_SUCCESS;
  45. }
  46. /**
  47. * @brief used to close the I2C or SPI serial port.
  48. * This port is used to send and receive data to the MPU device.
  49. * @return ML_SUCCESS if successful, a non-zero error code otherwise.
  50. */
  51. tMLError MLSLSerialClose(void *sl_handle)
  52. {
  53. return ML_SUCCESS;
  54. }
  55. /**
  56. * @brief used to read a single byte of data.
  57. * This should be sent by I2C or SPI.
  58. *
  59. * @param slaveAddr I2C slave address of device.
  60. * @param registerAddr Register address to read.
  61. * @param data Single byte of data to read.
  62. *
  63. * @return ML_SUCCESS if the command is successful, an error code otherwise.
  64. */
  65. tMLError MLSLSerialWriteSingle(void *sl_handle,
  66. unsigned char slaveAddr,
  67. unsigned char registerAddr,
  68. unsigned char data)
  69. {
  70. return sensor_i2c_write_register((struct i2c_adapter *) sl_handle,
  71. slaveAddr, registerAddr, data);
  72. }
  73. /**
  74. * @brief used to write multiple bytes of data from registers.
  75. * This should be sent by I2C.
  76. *
  77. * @param slaveAddr I2C slave address of device.
  78. * @param registerAddr Register address to write.
  79. * @param length Length of burst of data.
  80. * @param data Pointer to block of data.
  81. *
  82. * @return ML_SUCCESS if successful, a non-zero error code otherwise.
  83. */
  84. tMLError MLSLSerialWrite(void *sl_handle,
  85. unsigned char slaveAddr,
  86. unsigned short length,
  87. unsigned char const *data)
  88. {
  89. tMLError result;
  90. const unsigned short dataLength = length - 1;
  91. const unsigned char startRegAddr = data[0];
  92. unsigned char i2cWrite[SERIAL_MAX_TRANSFER_SIZE + 1];
  93. unsigned short bytesWritten = 0;
  94. while (bytesWritten < dataLength) {
  95. unsigned short thisLen = min(SERIAL_MAX_TRANSFER_SIZE,
  96. dataLength - bytesWritten);
  97. if (bytesWritten == 0) {
  98. result = sensor_i2c_write((struct i2c_adapter *)
  99. sl_handle, slaveAddr,
  100. 1 + thisLen, data);
  101. } else {
  102. /* manually increment register addr between chunks */
  103. i2cWrite[0] = startRegAddr + bytesWritten;
  104. memcpy(&i2cWrite[1], &data[1 + bytesWritten],
  105. thisLen);
  106. result = sensor_i2c_write((struct i2c_adapter *)
  107. sl_handle, slaveAddr,
  108. 1 + thisLen, i2cWrite);
  109. }
  110. if (ML_SUCCESS != result)
  111. return result;
  112. bytesWritten += thisLen;
  113. }
  114. return ML_SUCCESS;
  115. }
  116. /**
  117. * @brief used to read multiple bytes of data from registers.
  118. * This should be sent by I2C.
  119. *
  120. * @param slaveAddr I2C slave address of device.
  121. * @param registerAddr Register address to read.
  122. * @param length Length of burst of data.
  123. * @param data Pointer to block of data.
  124. *
  125. * @return Zero if successful; an error code otherwise
  126. */
  127. tMLError MLSLSerialRead(void *sl_handle,
  128. unsigned char slaveAddr,
  129. unsigned char registerAddr,
  130. unsigned short length, unsigned char *data)
  131. {
  132. tMLError result;
  133. unsigned short bytesRead = 0;
  134. if (registerAddr == MPUREG_FIFO_R_W
  135. || registerAddr == MPUREG_MEM_R_W) {
  136. return ML_ERROR_INVALID_PARAMETER;
  137. }
  138. while (bytesRead < length) {
  139. unsigned short thisLen =
  140. min(SERIAL_MAX_TRANSFER_SIZE, length - bytesRead);
  141. result =
  142. sensor_i2c_read((struct i2c_adapter *) sl_handle,
  143. slaveAddr, registerAddr + bytesRead,
  144. thisLen, &data[bytesRead]);
  145. if (ML_SUCCESS != result)
  146. return result;
  147. bytesRead += thisLen;
  148. }
  149. return ML_SUCCESS;
  150. }
  151. /**
  152. * @brief used to write multiple bytes of data to the memory.
  153. * This should be sent by I2C.
  154. *
  155. * @param slaveAddr I2C slave address of device.
  156. * @param memAddr The location in the memory to write to.
  157. * @param length Length of burst data.
  158. * @param data Pointer to block of data.
  159. *
  160. * @return Zero if successful; an error code otherwise
  161. */
  162. tMLError MLSLSerialWriteMem(void *sl_handle,
  163. unsigned char slaveAddr,
  164. unsigned short memAddr,
  165. unsigned short length,
  166. unsigned char const *data)
  167. {
  168. tMLError result;
  169. unsigned short bytesWritten = 0;
  170. if ((memAddr & 0xFF) + length > MPU_MEM_BANK_SIZE) {
  171. pr_err("memory read length (%d B) extends beyond its"
  172. " limits (%d) if started at location %d\n", length,
  173. MPU_MEM_BANK_SIZE, memAddr & 0xFF);
  174. return ML_ERROR_INVALID_PARAMETER;
  175. }
  176. while (bytesWritten < length) {
  177. unsigned short thisLen =
  178. min(SERIAL_MAX_TRANSFER_SIZE, length - bytesWritten);
  179. result =
  180. mpu_memory_write((struct i2c_adapter *) sl_handle,
  181. slaveAddr, memAddr + bytesWritten,
  182. thisLen, &data[bytesWritten]);
  183. if (ML_SUCCESS != result)
  184. return result;
  185. bytesWritten += thisLen;
  186. }
  187. return ML_SUCCESS;
  188. }
  189. /**
  190. * @brief used to read multiple bytes of data from the memory.
  191. * This should be sent by I2C.
  192. *
  193. * @param slaveAddr I2C slave address of device.
  194. * @param memAddr The location in the memory to read from.
  195. * @param length Length of burst data.
  196. * @param data Pointer to block of data.
  197. *
  198. * @return Zero if successful; an error code otherwise
  199. */
  200. tMLError MLSLSerialReadMem(void *sl_handle,
  201. unsigned char slaveAddr,
  202. unsigned short memAddr,
  203. unsigned short length, unsigned char *data)
  204. {
  205. tMLError result;
  206. unsigned short bytesRead = 0;
  207. if ((memAddr & 0xFF) + length > MPU_MEM_BANK_SIZE) {
  208. printk
  209. ("memory read length (%d B) extends beyond its limits (%d) "
  210. "if started at location %d\n", length,
  211. MPU_MEM_BANK_SIZE, memAddr & 0xFF);
  212. return ML_ERROR_INVALID_PARAMETER;
  213. }
  214. while (bytesRead < length) {
  215. unsigned short thisLen =
  216. min(SERIAL_MAX_TRANSFER_SIZE, length - bytesRead);
  217. result =
  218. mpu_memory_read((struct i2c_adapter *) sl_handle,
  219. slaveAddr, memAddr + bytesRead,
  220. thisLen, &data[bytesRead]);
  221. if (ML_SUCCESS != result)
  222. return result;
  223. bytesRead += thisLen;
  224. }
  225. return ML_SUCCESS;
  226. }
  227. /**
  228. * @brief used to write multiple bytes of data to the fifo.
  229. * This should be sent by I2C.
  230. *
  231. * @param slaveAddr I2C slave address of device.
  232. * @param length Length of burst of data.
  233. * @param data Pointer to block of data.
  234. *
  235. * @return Zero if successful; an error code otherwise
  236. */
  237. tMLError MLSLSerialWriteFifo(void *sl_handle,
  238. unsigned char slaveAddr,
  239. unsigned short length,
  240. unsigned char const *data)
  241. {
  242. tMLError result;
  243. unsigned char i2cWrite[SERIAL_MAX_TRANSFER_SIZE + 1];
  244. unsigned short bytesWritten = 0;
  245. if (length > FIFO_HW_SIZE) {
  246. printk(KERN_ERR
  247. "maximum fifo write length is %d\n", FIFO_HW_SIZE);
  248. return ML_ERROR_INVALID_PARAMETER;
  249. }
  250. while (bytesWritten < length) {
  251. unsigned short thisLen =
  252. min(SERIAL_MAX_TRANSFER_SIZE, length - bytesWritten);
  253. i2cWrite[0] = MPUREG_FIFO_R_W;
  254. memcpy(&i2cWrite[1], &data[bytesWritten], thisLen);
  255. result = sensor_i2c_write((struct i2c_adapter *) sl_handle,
  256. slaveAddr, thisLen + 1,
  257. i2cWrite);
  258. if (ML_SUCCESS != result)
  259. return result;
  260. bytesWritten += thisLen;
  261. }
  262. return ML_SUCCESS;
  263. }
  264. /**
  265. * @brief used to read multiple bytes of data from the fifo.
  266. * This should be sent by I2C.
  267. *
  268. * @param slaveAddr I2C slave address of device.
  269. * @param length Length of burst of data.
  270. * @param data Pointer to block of data.
  271. *
  272. * @return Zero if successful; an error code otherwise
  273. */
  274. tMLError MLSLSerialReadFifo(void *sl_handle,
  275. unsigned char slaveAddr,
  276. unsigned short length, unsigned char *data)
  277. {
  278. tMLError result;
  279. unsigned short bytesRead = 0;
  280. if (length > FIFO_HW_SIZE) {
  281. printk(KERN_ERR
  282. "maximum fifo read length is %d\n", FIFO_HW_SIZE);
  283. return ML_ERROR_INVALID_PARAMETER;
  284. }
  285. while (bytesRead < length) {
  286. unsigned short thisLen =
  287. min(SERIAL_MAX_TRANSFER_SIZE, length - bytesRead);
  288. result =
  289. sensor_i2c_read((struct i2c_adapter *) sl_handle,
  290. slaveAddr, MPUREG_FIFO_R_W, thisLen,
  291. &data[bytesRead]);
  292. if (ML_SUCCESS != result)
  293. return result;
  294. bytesRead += thisLen;
  295. }
  296. return ML_SUCCESS;
  297. }
  298. /**
  299. * @}
  300. */