ami30x.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 COMPASSDL (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 ami30x.c
  23. * @brief Magnetometer setup and handling methods for Aichi AMI304/AMI305
  24. * compass.
  25. */
  26. /* ------------------ */
  27. /* - Include Files. - */
  28. /* ------------------ */
  29. #ifdef __KERNEL__
  30. #include <linux/module.h>
  31. #endif
  32. #include "mpu.h"
  33. #include "mlsl.h"
  34. #include "mlos.h"
  35. #include <log.h>
  36. #undef MPL_LOG_TAG
  37. #define MPL_LOG_TAG "MPL-compass"
  38. #define AMI30X_REG_DATAX (0x10)
  39. #define AMI30X_REG_STAT1 (0x18)
  40. #define AMI30X_REG_CNTL1 (0x1B)
  41. #define AMI30X_REG_CNTL2 (0x1C)
  42. #define AMI30X_REG_CNTL3 (0x1D)
  43. #define AMI30X_BIT_CNTL1_PC1 (0x80)
  44. #define AMI30X_BIT_CNTL1_ODR1 (0x10)
  45. #define AMI30X_BIT_CNTL1_FS1 (0x02)
  46. #define AMI30X_BIT_CNTL2_IEN (0x10)
  47. #define AMI30X_BIT_CNTL2_DREN (0x08)
  48. #define AMI30X_BIT_CNTL2_DRP (0x04)
  49. #define AMI30X_BIT_CNTL3_F0RCE (0x40)
  50. int ami30x_suspend(void *mlsl_handle,
  51. struct ext_slave_descr *slave,
  52. struct ext_slave_platform_data *pdata)
  53. {
  54. int result;
  55. unsigned char reg;
  56. result =
  57. MLSLSerialRead(mlsl_handle, pdata->address, AMI30X_REG_CNTL1,
  58. 1, &reg);
  59. ERROR_CHECK(result);
  60. reg &= ~(AMI30X_BIT_CNTL1_PC1|AMI30X_BIT_CNTL1_FS1);
  61. result =
  62. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  63. AMI30X_REG_CNTL1, reg);
  64. ERROR_CHECK(result);
  65. return result;
  66. }
  67. int ami30x_resume(void *mlsl_handle,
  68. struct ext_slave_descr *slave,
  69. struct ext_slave_platform_data *pdata)
  70. {
  71. int result = ML_SUCCESS;
  72. /* Set CNTL1 reg to power model active */
  73. result =
  74. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  75. AMI30X_REG_CNTL1,
  76. AMI30X_BIT_CNTL1_PC1|AMI30X_BIT_CNTL1_FS1);
  77. ERROR_CHECK(result);
  78. /* Set CNTL2 reg to DRDY active high and enabled */
  79. result =
  80. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  81. AMI30X_REG_CNTL2,
  82. AMI30X_BIT_CNTL2_DREN |
  83. AMI30X_BIT_CNTL2_DRP);
  84. ERROR_CHECK(result);
  85. /* Set CNTL3 reg to forced measurement period */
  86. result =
  87. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  88. AMI30X_REG_CNTL3, AMI30X_BIT_CNTL3_F0RCE);
  89. return result;
  90. }
  91. int ami30x_read(void *mlsl_handle,
  92. struct ext_slave_descr *slave,
  93. struct ext_slave_platform_data *pdata, unsigned char *data)
  94. {
  95. unsigned char stat;
  96. int result = ML_SUCCESS;
  97. /* Read status reg and check if data ready (DRDY) */
  98. result =
  99. MLSLSerialRead(mlsl_handle, pdata->address, AMI30X_REG_STAT1,
  100. 1, &stat);
  101. ERROR_CHECK(result);
  102. if (stat & 0x40) {
  103. result =
  104. MLSLSerialRead(mlsl_handle, pdata->address,
  105. AMI30X_REG_DATAX, 6,
  106. (unsigned char *) data);
  107. ERROR_CHECK(result);
  108. /* start another measurement */
  109. result =
  110. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  111. AMI30X_REG_CNTL3,
  112. AMI30X_BIT_CNTL3_F0RCE);
  113. ERROR_CHECK(result);
  114. return ML_SUCCESS;
  115. }
  116. return ML_ERROR_COMPASS_DATA_NOT_READY;
  117. }
  118. struct ext_slave_descr ami30x_descr = {
  119. /*.init = */ NULL,
  120. /*.exit = */ NULL,
  121. /*.suspend = */ ami30x_suspend,
  122. /*.resume = */ ami30x_resume,
  123. /*.read = */ ami30x_read,
  124. /*.config = */ NULL,
  125. /*.get_config = */ NULL,
  126. /*.name = */ "ami30x",
  127. /*.type = */ EXT_SLAVE_TYPE_COMPASS,
  128. /*.id = */ COMPASS_ID_AMI30X,
  129. /*.reg = */ 0x06,
  130. /*.len = */ 6,
  131. /*.endian = */ EXT_SLAVE_LITTLE_ENDIAN,
  132. /*.range = */ {5461, 3333}
  133. /* For AMI305,the range field needs to be modified to {9830.4f}*/
  134. };
  135. struct ext_slave_descr *ami30x_get_slave_descr(void)
  136. {
  137. return &ami30x_descr;
  138. }
  139. EXPORT_SYMBOL(ami30x_get_slave_descr);
  140. /**
  141. * @}
  142. **/