ami306.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 ami306.c
  23. * @brief Magnetometer setup and handling methods for Aichi AMI306
  24. * compass.
  25. */
  26. /* ------------------ */
  27. /* - Include Files. - */
  28. /* ------------------ */
  29. #ifdef __KERNEL__
  30. #include <linux/module.h>
  31. #endif
  32. #include <delay.h>
  33. #include "mpu.h"
  34. #include "mlsl.h"
  35. #include "mlos.h"
  36. #include <log.h>
  37. #undef MPL_LOG_TAG
  38. #define MPL_LOG_TAG "MPL-compass"
  39. #define AMI306_REG_DATAX (0x10)
  40. #define AMI306_REG_STAT1 (0x18)
  41. #define AMI306_REG_CNTL1 (0x1B)
  42. #define AMI306_REG_CNTL2 (0x1C)
  43. #define AMI306_REG_CNTL3 (0x1D)
  44. #define AMI306_REG_CNTL4_1 (0x5C)
  45. #define AMI306_REG_CNTL4_2 (0x5D)
  46. #define AMI306_BIT_CNTL1_PC1 (0x80)
  47. #define AMI306_BIT_CNTL1_ODR1 (0x10)
  48. #define AMI306_BIT_CNTL1_FS1 (0x02)
  49. #define AMI306_BIT_CNTL2_IEN (0x10)
  50. #define AMI306_BIT_CNTL2_DREN (0x08)
  51. #define AMI306_BIT_CNTL2_DRP (0x04)
  52. #define AMI306_BIT_CNTL3_F0RCE (0x40)
  53. int ami306_suspend(void *mlsl_handle,
  54. struct ext_slave_descr *slave,
  55. struct ext_slave_platform_data *pdata)
  56. {
  57. int result;
  58. unsigned char reg;
  59. result =
  60. MLSLSerialRead(mlsl_handle, pdata->address, AMI306_REG_CNTL1,
  61. 1, &reg);
  62. ERROR_CHECK(result);
  63. reg &= ~(AMI306_BIT_CNTL1_PC1|AMI306_BIT_CNTL1_FS1);
  64. result =
  65. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  66. AMI306_REG_CNTL1, reg);
  67. ERROR_CHECK(result);
  68. return result;
  69. }
  70. int ami306_resume(void *mlsl_handle,
  71. struct ext_slave_descr *slave,
  72. struct ext_slave_platform_data *pdata)
  73. {
  74. int result = ML_SUCCESS;
  75. unsigned char regs[] = {
  76. AMI306_REG_CNTL4_1,
  77. 0x7E,
  78. 0xA0
  79. };
  80. /* Step1. Set CNTL1 reg to power model active (Write CNTL1:PC1=1) */
  81. result =
  82. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  83. AMI306_REG_CNTL1,
  84. AMI306_BIT_CNTL1_PC1|AMI306_BIT_CNTL1_FS1);
  85. ERROR_CHECK(result);
  86. /* Step2. Set CNTL2 reg to DRDY active high and enabled
  87. (Write CNTL2:DREN=1) */
  88. result =
  89. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  90. AMI306_REG_CNTL2,
  91. AMI306_BIT_CNTL2_DREN |
  92. AMI306_BIT_CNTL2_DRP);
  93. ERROR_CHECK(result);
  94. /* Step3. Set CNTL4 reg to for measurement speed (Write CNTL4, 0xA07E) */
  95. result =
  96. MLSLSerialWrite(mlsl_handle, pdata->address, DIM(regs), regs);
  97. ERROR_CHECK(result);
  98. /* Step4. skipped */
  99. /* Step5. Set CNTL3 reg to forced measurement period
  100. (Write CNTL3:FORCE=1) */
  101. result =
  102. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  103. AMI306_REG_CNTL3, AMI306_BIT_CNTL3_F0RCE);
  104. return result;
  105. }
  106. int ami306_read(void *mlsl_handle,
  107. struct ext_slave_descr *slave,
  108. struct ext_slave_platform_data *pdata, unsigned char *data)
  109. {
  110. unsigned char stat;
  111. int result = ML_SUCCESS;
  112. int status = ML_SUCCESS;
  113. /* Measurement(x,y,z) */
  114. result =
  115. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  116. AMI306_REG_CNTL3,
  117. AMI306_BIT_CNTL3_F0RCE);
  118. ERROR_CHECK(result);
  119. udelay(500);
  120. /* Step6. Read status reg and check if data ready (DRDY) */
  121. result =
  122. MLSLSerialRead(mlsl_handle, pdata->address, AMI306_REG_STAT1,
  123. 1, &stat);
  124. ERROR_CHECK(result);
  125. /* Step6. Does DRDY output the rising edge? */
  126. if (stat & 0x40) {
  127. result =
  128. MLSLSerialRead(mlsl_handle, pdata->address,
  129. AMI306_REG_DATAX, 6,
  130. (unsigned char *) data);
  131. ERROR_CHECK(result);
  132. status = ML_SUCCESS;
  133. } else if (stat & 0x20)
  134. status = ML_ERROR_COMPASS_DATA_OVERFLOW;
  135. return status;
  136. }
  137. struct ext_slave_descr ami306_descr = {
  138. /*.init = */ NULL,
  139. /*.exit = */ NULL,
  140. /*.suspend = */ ami306_suspend,
  141. /*.resume = */ ami306_resume,
  142. /*.read = */ ami306_read,
  143. /*.config = */ NULL,
  144. /*.get_config = */ NULL,
  145. /*.name = */ "ami306",
  146. /*.type = */ EXT_SLAVE_TYPE_COMPASS,
  147. /*.id = */ COMPASS_ID_AMI306,
  148. /*.reg = */ 0x10,
  149. /*.len = */ 6,
  150. /*.endian = */ EXT_SLAVE_LITTLE_ENDIAN,
  151. /*.range = */ {5461, 3333}
  152. /* For AMI305,the range field needs to be modified to {9830.4f}*/
  153. };
  154. struct ext_slave_descr *ami306_get_slave_descr(void)
  155. {
  156. return &ami306_descr;
  157. }
  158. EXPORT_SYMBOL(ami306_get_slave_descr);
  159. /**
  160. * @}
  161. */