hscdtd004a.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * @brief Provides the interface to setup and handle a compass
  18. * connected to the primary I2C interface of the gyroscope.
  19. *
  20. * @{
  21. * @file hscdtd004a.c
  22. * @brief Magnetometer setup and handling methods for Alps hscdtd004a
  23. * 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. /*----- ALPS HSCDTD004A Registers ------*/
  38. #define COMPASS_HSCDTD004A_STAT (0x18)
  39. #define COMPASS_HSCDTD004A_CTRL1 (0x1B)
  40. #define COMPASS_HSCDTD004A_CTRL2 (0x1C)
  41. #define COMPASS_HSCDTD004A_CTRL3 (0x1D)
  42. #define COMPASS_HSCDTD004A_DATAX (0x10)
  43. /* --------------------- */
  44. /* - Variables. - */
  45. /* --------------------- */
  46. /*****************************************
  47. Compass Initialization Functions
  48. *****************************************/
  49. int hscdtd004a_suspend(void *mlsl_handle,
  50. struct ext_slave_descr *slave,
  51. struct ext_slave_platform_data *pdata)
  52. {
  53. int result = ML_SUCCESS;
  54. /* Power mode: stand-by */
  55. result =
  56. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  57. COMPASS_HSCDTD004A_CTRL1, 0x00);
  58. ERROR_CHECK(result);
  59. MLOSSleep(1); /* turn-off time */
  60. return result;
  61. }
  62. int hscdtd004a_resume(void *mlsl_handle,
  63. struct ext_slave_descr *slave,
  64. struct ext_slave_platform_data *pdata)
  65. {
  66. int result = ML_SUCCESS;
  67. /* Soft reset */
  68. result =
  69. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  70. COMPASS_HSCDTD004A_CTRL3, 0x80);
  71. ERROR_CHECK(result);
  72. /* Normal state; Power mode: active */
  73. result =
  74. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  75. COMPASS_HSCDTD004A_CTRL1, 0x82);
  76. ERROR_CHECK(result);
  77. /* Data ready enable */
  78. result =
  79. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  80. COMPASS_HSCDTD004A_CTRL2, 0x7C);
  81. ERROR_CHECK(result);
  82. MLOSSleep(1); /* turn-on time */
  83. return result;
  84. }
  85. int hscdtd004a_read(void *mlsl_handle,
  86. struct ext_slave_descr *slave,
  87. struct ext_slave_platform_data *pdata,
  88. unsigned char *data)
  89. {
  90. unsigned char stat;
  91. tMLError result = ML_SUCCESS;
  92. int status = ML_SUCCESS;
  93. /* Read status reg. to check if data is ready */
  94. result =
  95. MLSLSerialRead(mlsl_handle, pdata->address,
  96. COMPASS_HSCDTD004A_STAT, 1, &stat);
  97. ERROR_CHECK(result);
  98. if (stat & 0x48) {
  99. result =
  100. MLSLSerialRead(mlsl_handle, pdata->address,
  101. COMPASS_HSCDTD004A_DATAX, 6,
  102. (unsigned char *) data);
  103. ERROR_CHECK(result);
  104. status = ML_SUCCESS;
  105. } else if (stat & 0x68) {
  106. status = ML_ERROR_COMPASS_DATA_OVERFLOW;
  107. } else {
  108. status = ML_ERROR_COMPASS_DATA_NOT_READY;
  109. }
  110. /* trigger next measurement read */
  111. result =
  112. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  113. COMPASS_HSCDTD004A_CTRL3, 0x40);
  114. ERROR_CHECK(result);
  115. return status;
  116. }
  117. struct ext_slave_descr hscdtd004a_descr = {
  118. /*.init = */ NULL,
  119. /*.exit = */ NULL,
  120. /*.suspend = */ hscdtd004a_suspend,
  121. /*.resume = */ hscdtd004a_resume,
  122. /*.read = */ hscdtd004a_read,
  123. /*.config = */ NULL,
  124. /*.get_config = */ NULL,
  125. /*.name = */ "hscdtd004a",
  126. /*.type = */ EXT_SLAVE_TYPE_COMPASS,
  127. /*.id = */ COMPASS_ID_HSCDTD004A,
  128. /*.reg = */ 0x10,
  129. /*.len = */ 6,
  130. /*.endian = */ EXT_SLAVE_LITTLE_ENDIAN,
  131. /*.range = */ {9830, 4000},
  132. };
  133. struct ext_slave_descr *hscdtd004a_get_slave_descr(void)
  134. {
  135. return &hscdtd004a_descr;
  136. }
  137. EXPORT_SYMBOL(hscdtd004a_get_slave_descr);
  138. /**
  139. * @}
  140. **/