mmc314x.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 ACCELDL (Motion Library - Accelerometer Driver Layer)
  18. * @brief Provides the interface to setup and handle an accelerometers
  19. * connected to the secondary I2C interface of the gyroscope.
  20. *
  21. * @{
  22. * @file mmc314x.c
  23. * @brief Magnetometer setup and handling methods for ???? compass.
  24. */
  25. /* ------------------ */
  26. /* - Include Files. - */
  27. /* ------------------ */
  28. #ifdef __KERNEL__
  29. #include <linux/module.h>
  30. #endif
  31. #include "mpu.h"
  32. #include "mlsl.h"
  33. #include "mlos.h"
  34. #include <log.h>
  35. #undef MPL_LOG_TAG
  36. #define MPL_LOG_TAG "MPL-compass"
  37. /* --------------------- */
  38. /* - Variables. - */
  39. /* --------------------- */
  40. static int reset_int = 1000;
  41. static int read_count = 1;
  42. static char reset_mode; /* in Z-init section */
  43. #define MMC314X_REG_ST (0x00)
  44. #define MMC314X_REG_X_MSB (0x01)
  45. #define MMC314X_CNTL_MODE_WAKE_UP (0x01)
  46. #define MMC314X_CNTL_MODE_SET (0x02)
  47. #define MMC314X_CNTL_MODE_RESET (0x04)
  48. /*****************************************
  49. Accelerometer Initialization Functions
  50. *****************************************/
  51. int mmc314x_suspend(void *mlsl_handle,
  52. struct ext_slave_descr *slave,
  53. struct ext_slave_platform_data *pdata)
  54. {
  55. int result = ML_SUCCESS;
  56. return result;
  57. }
  58. int mmc314x_resume(void *mlsl_handle,
  59. struct ext_slave_descr *slave,
  60. struct ext_slave_platform_data *pdata)
  61. {
  62. int result;
  63. result =
  64. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  65. MMC314X_REG_ST, MMC314X_CNTL_MODE_RESET);
  66. ERROR_CHECK(result);
  67. MLOSSleep(10);
  68. result =
  69. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  70. MMC314X_REG_ST, MMC314X_CNTL_MODE_SET);
  71. ERROR_CHECK(result);
  72. MLOSSleep(10);
  73. read_count = 1;
  74. return ML_SUCCESS;
  75. }
  76. int mmc314x_read(void *mlsl_handle,
  77. struct ext_slave_descr *slave,
  78. struct ext_slave_platform_data *pdata,
  79. unsigned char *data)
  80. {
  81. int result, ii;
  82. short tmp[3];
  83. unsigned char tmpdata[6];
  84. if (read_count > 1000)
  85. read_count = 1;
  86. result =
  87. MLSLSerialRead(mlsl_handle, pdata->address, MMC314X_REG_X_MSB,
  88. 6, (unsigned char *) data);
  89. ERROR_CHECK(result);
  90. for (ii = 0; ii < 6; ii++)
  91. tmpdata[ii] = data[ii];
  92. for (ii = 0; ii < 3; ii++) {
  93. tmp[ii] =
  94. (short) ((tmpdata[2 * ii] << 8) + tmpdata[2 * ii + 1]);
  95. tmp[ii] = tmp[ii] - 4096;
  96. tmp[ii] = tmp[ii] * 16;
  97. }
  98. for (ii = 0; ii < 3; ii++) {
  99. data[2 * ii] = (unsigned char) (tmp[ii] >> 8);
  100. data[2 * ii + 1] = (unsigned char) (tmp[ii]);
  101. }
  102. if (read_count % reset_int == 0) {
  103. if (reset_mode) {
  104. result =
  105. MLSLSerialWriteSingle(mlsl_handle,
  106. pdata->address,
  107. MMC314X_REG_ST,
  108. MMC314X_CNTL_MODE_RESET);
  109. ERROR_CHECK(result);
  110. reset_mode = 0;
  111. return ML_ERROR_COMPASS_DATA_NOT_READY;
  112. } else {
  113. result =
  114. MLSLSerialWriteSingle(mlsl_handle,
  115. pdata->address,
  116. MMC314X_REG_ST,
  117. MMC314X_CNTL_MODE_SET);
  118. ERROR_CHECK(result);
  119. reset_mode = 1;
  120. read_count++;
  121. return ML_ERROR_COMPASS_DATA_NOT_READY;
  122. }
  123. }
  124. result =
  125. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  126. MMC314X_REG_ST,
  127. MMC314X_CNTL_MODE_WAKE_UP);
  128. ERROR_CHECK(result);
  129. read_count++;
  130. return ML_SUCCESS;
  131. }
  132. struct ext_slave_descr mmc314x_descr = {
  133. /*.init = */ NULL,
  134. /*.exit = */ NULL,
  135. /*.suspend = */ mmc314x_suspend,
  136. /*.resume = */ mmc314x_resume,
  137. /*.read = */ mmc314x_read,
  138. /*.config = */ NULL,
  139. /*.get_config = */ NULL,
  140. /*.name = */ "mmc314x",
  141. /*.type = */ EXT_SLAVE_TYPE_COMPASS,
  142. /*.id = */ COMPASS_ID_MMC314X,
  143. /*.reg = */ 0x01,
  144. /*.len = */ 6,
  145. /*.endian = */ EXT_SLAVE_BIG_ENDIAN,
  146. /*.range = */ {400, 0},
  147. };
  148. struct ext_slave_descr *mmc314x_get_slave_descr(void)
  149. {
  150. return &mmc314x_descr;
  151. }
  152. EXPORT_SYMBOL(mmc314x_get_slave_descr);
  153. /**
  154. * @}
  155. **/