aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/mpu3050/compass/hmc5883.c
blob: 051b071fe7fbcffb418628689dbe7e2da63b9646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 $License:
    Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  $
 */

/**
 *  @brief      Provides the interface to setup and handle a compass
 *              connected to the primary I2C interface of the gyroscope.
 *
 *  @{
 *      @file   hmc5883.c
 *      @brief  Magnetometer setup and handling methods for honeywell hmc5883
 *              compass.
 */

/* ------------------ */
/* - Include Files. - */
/* ------------------ */

#ifdef __KERNEL__
#include <linux/module.h>
#endif

#include "mpu.h"
#include "mlsl.h"
#include "mlos.h"

#include <log.h>
#undef MPL_LOG_TAG
#define MPL_LOG_TAG "MPL-compass"

/*-----HONEYWELL HMC5883 Registers ------*/
enum HMC_REG {
	HMC_REG_CONF_A = 0x0,
	HMC_REG_CONF_B = 0x1,
	HMC_REG_MODE = 0x2,
	HMC_REG_X_M = 0x3,
	HMC_REG_X_L = 0x4,
	HMC_REG_Z_M = 0x5,
	HMC_REG_Z_L = 0x6,
	HMC_REG_Y_M = 0x7,
	HMC_REG_Y_L = 0x8,
	HMC_REG_STATUS = 0x9,
	HMC_REG_ID_A = 0xA,
	HMC_REG_ID_B = 0xB,
	HMC_REG_ID_C = 0xC
};

enum HMC_CONF_A {
	HMC_CONF_A_DRATE_MASK = 0x1C,
	HMC_CONF_A_DRATE_0_75 = 0x00,
	HMC_CONF_A_DRATE_1_5 = 0x04,
	HMC_CONF_A_DRATE_3 = 0x08,
	HMC_CONF_A_DRATE_7_5 = 0x0C,
	HMC_CONF_A_DRATE_15 = 0x10,
	HMC_CONF_A_DRATE_30 = 0x14,
	HMC_CONF_A_DRATE_75 = 0x18,
	HMC_CONF_A_MEAS_MASK = 0x3,
	HMC_CONF_A_MEAS_NORM = 0x0,
	HMC_CONF_A_MEAS_POS = 0x1,
	HMC_CONF_A_MEAS_NEG = 0x2
};

enum HMC_CONF_B {
	HMC_CONF_B_GAIN_MASK = 0xE0,
	HMC_CONF_B_GAIN_0_9 = 0x00,
	HMC_CONF_B_GAIN_1_2 = 0x20,
	HMC_CONF_B_GAIN_1_9 = 0x40,
	HMC_CONF_B_GAIN_2_5 = 0x60,
	HMC_CONF_B_GAIN_4_0 = 0x80,
	HMC_CONF_B_GAIN_4_6 = 0xA0,
	HMC_CONF_B_GAIN_5_5 = 0xC0,
	HMC_CONF_B_GAIN_7_9 = 0xE0
};

enum HMC_MODE {
	HMC_MODE_MASK = 0x3,
	HMC_MODE_CONT = 0x0,
	HMC_MODE_SINGLE = 0x1,
	HMC_MODE_IDLE = 0x2,
	HMC_MODE_SLEEP = 0x3
};

/* --------------------- */
/* -    Variables.     - */
/* --------------------- */

/*****************************************
    Accelerometer Initialization Functions
*****************************************/

int hmc5883_suspend(void *mlsl_handle,
		    struct ext_slave_descr *slave,
		    struct ext_slave_platform_data *pdata)
{
	int result = ML_SUCCESS;

	result =
	    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
				  HMC_REG_MODE, HMC_MODE_SLEEP);
	ERROR_CHECK(result);
	MLOSSleep(3);

	return result;
}

int hmc5883_resume(void *mlsl_handle,
		   struct ext_slave_descr *slave,
		   struct ext_slave_platform_data *pdata)
{
	int result = ML_SUCCESS;

	/* Use single measurement mode. Start at sleep state. */
	result =
	    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
				  HMC_REG_MODE, HMC_MODE_SLEEP);
	ERROR_CHECK(result);
	/* Config normal measurement */
	result =
	    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
				  HMC_REG_CONF_A, 0);
	ERROR_CHECK(result);
	/* Adjust gain to 307 LSB/Gauss */
	result =
	    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
				  HMC_REG_CONF_B, HMC_CONF_B_GAIN_5_5);
	ERROR_CHECK(result);

	return result;
}

int hmc5883_read(void *mlsl_handle,
		 struct ext_slave_descr *slave,
		 struct ext_slave_platform_data *pdata,
		 unsigned char *data)
{
	unsigned char stat;
	tMLError result = ML_SUCCESS;
	unsigned char tmp;
	short axisFixed;

	/* Read status reg. to check if data is ready */
	result =
	    MLSLSerialRead(mlsl_handle, pdata->address, HMC_REG_STATUS, 1,
			   &stat);
	ERROR_CHECK(result);
	if (stat & 0x01) {
		result =
		    MLSLSerialRead(mlsl_handle, pdata->address,
				   HMC_REG_X_M, 6, (unsigned char *) data);
		ERROR_CHECK(result);

		/* switch YZ axis to proper position */
		tmp = data[2];
		data[2] = data[4];
		data[4] = tmp;
		tmp = data[3];
		data[3] = data[5];
		data[5] = tmp;

		/*drop data if overflows */
		if ((data[0] == 0xf0) || (data[2] == 0xf0)
		    || (data[4] == 0xf0)) {
			/* trigger next measurement read */
			result =
			    MLSLSerialWriteSingle(mlsl_handle,
							pdata->address,
							HMC_REG_MODE,
							HMC_MODE_SINGLE);
			ERROR_CHECK(result);
			return ML_ERROR_COMPASS_DATA_OVERFLOW;
		}
		/* convert to fixed point and apply sensitivity correction for
		   Z-axis */
		axisFixed =
		    (short) ((unsigned short) data[5] +
			     (unsigned short) data[4] * 256);
		/* scale up by 1.125 (36/32) */
		axisFixed = (short) (axisFixed * 36);
		data[4] = axisFixed >> 8;
		data[5] = axisFixed & 0xFF;

		axisFixed =
		    (short) ((unsigned short) data[3] +
			     (unsigned short) data[2] * 256);
		axisFixed = (short) (axisFixed * 32);
		data[2] = axisFixed >> 8;
		data[3] = axisFixed & 0xFF;

		axisFixed =
		    (short) ((unsigned short) data[1] +
			     (unsigned short) data[0] * 256);
		axisFixed = (short) (axisFixed * 32);
		data[0] = axisFixed >> 8;
		data[1] = axisFixed & 0xFF;

		/* trigger next measurement read */
		result =
		    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
					  HMC_REG_MODE, HMC_MODE_SINGLE);
		ERROR_CHECK(result);

		return ML_SUCCESS;
	} else {
		/* trigger next measurement read */
		result =
		    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
					  HMC_REG_MODE, HMC_MODE_SINGLE);
		ERROR_CHECK(result);

		return ML_ERROR_COMPASS_DATA_NOT_READY;
	}
}

struct ext_slave_descr hmc5883_descr = {
	/*.init             = */ NULL,
	/*.exit             = */ NULL,
	/*.suspend          = */ hmc5883_suspend,
	/*.resume           = */ hmc5883_resume,
	/*.read             = */ hmc5883_read,
	/*.config           = */ NULL,
	/*.get_config       = */ NULL,
	/*.name             = */ "hmc5883",
	/*.type             = */ EXT_SLAVE_TYPE_COMPASS,
	/*.id               = */ COMPASS_ID_HMC5883,
	/*.reg              = */ 0x06,
	/*.len              = */ 6,
	/*.endian           = */ EXT_SLAVE_BIG_ENDIAN,
	/*.range            = */ {10673, 6156},
};

struct ext_slave_descr *hmc5883_get_slave_descr(void)
{
	return &hmc5883_descr;
}
EXPORT_SYMBOL(hmc5883_get_slave_descr);

/**
 *  @}
**/