aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/mpu3050/accel/mma845x.c
blob: 27150ad86994a4bd675df6f0f656f67c39fefd90 (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
/*
 $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/>.
  $
 */

/**
 *  @defgroup   ACCELDL (Motion Library - Accelerometer Driver Layer)
 *  @brief      Provides the interface to setup and handle an accelerometers
 *              connected to the secondary I2C interface of the gyroscope.
 *
 *  @{
 *      @file   mma845x.c
 *      @brief  Accelerometer setup and handling methods for Freescale MMA845X
 */

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

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

#include <stdlib.h>
#include "mpu.h"
#include "mlsl.h"
#include "mlos.h"
#include <string.h>

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

#define ACCEL_MMA845X_CTRL_REG1      (0x2A)
#define ACCEL_MMA845X_SLEEP_MASK     (0x01)

/* full scale setting - register & mask */
#define ACCEL_MMA845X_CFG_REG       (0x0E)
#define ACCEL_MMA845X_CTRL_MASK     (0x03)

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

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

int mma845x_suspend(void *mlsl_handle,
		    struct ext_slave_descr *slave,
		    struct ext_slave_platform_data *pdata)
{
	int result;
	unsigned char reg;
	result =
	    MLSLSerialRead(mlsl_handle, pdata->address,
			   ACCEL_MMA845X_CTRL_REG1, 1, &reg);
	ERROR_CHECK(result);
	reg &= ~ACCEL_MMA845X_SLEEP_MASK;
	result =
	    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
				  ACCEL_MMA845X_CTRL_REG1, reg);
	ERROR_CHECK(result);
	return result;
}


int mma845x_resume(void *mlsl_handle,
		   struct ext_slave_descr *slave,
		   struct ext_slave_platform_data *pdata)
{
	int result = ML_SUCCESS;
	unsigned char reg;

	result = MLSLSerialRead(mlsl_handle, pdata->address,
				ACCEL_MMA845X_CFG_REG, 1, &reg);
	ERROR_CHECK(result);

	/* data rate = 200Hz */

	/* Full Scale */
	reg &= ~ACCEL_MMA845X_CTRL_MASK;
	if (slave->range.mantissa == 4)
		reg |= 0x1;
	else if (slave->range.mantissa == 8)
		reg |= 0x2;
	else {
		slave->range.mantissa = 2;
		reg |= 0x0;
	}
	slave->range.fraction = 0;

	result = MLSLSerialWriteSingle(mlsl_handle, pdata->address,
				ACCEL_MMA845X_CFG_REG, reg);
	ERROR_CHECK(result);
	/* 200Hz + active mode */
	result =
	    MLSLSerialWriteSingle(mlsl_handle, pdata->address,
			    ACCEL_MMA845X_CTRL_REG1, 0x11);
	ERROR_CHECK(result);

	return result;
}

int mma845x_read(void *mlsl_handle,
		 struct ext_slave_descr *slave,
		 struct ext_slave_platform_data *pdata,
		 unsigned char *data)
{
	int result;
	unsigned char local_data[7]; /* Status register + 6 bytes data */
	result = MLSLSerialRead(mlsl_handle, pdata->address,
				slave->reg, sizeof(local_data), local_data);
	ERROR_CHECK(result);
	memcpy(data, &local_data[1], slave->len);
	return result;
}

struct ext_slave_descr mma845x_descr = {
	/*.init             = */ NULL,
	/*.exit             = */ NULL,
	/*.suspend          = */ mma845x_suspend,
	/*.resume           = */ mma845x_resume,
	/*.read             = */ mma845x_read,
	/*.config           = */ NULL,
	/*.get_config       = */ NULL,
	/*.name             = */ "mma845x",
	/*.type             = */ EXT_SLAVE_TYPE_ACCELEROMETER,
	/*.id               = */ ACCEL_ID_MMA845X,
	/*.reg              = */ 0x00,
	/*.len              = */ 6,
	/*.endian           = */ EXT_SLAVE_FS16_BIG_ENDIAN,
	/*.range            = */ {2, 0},
};

struct ext_slave_descr *mma845x_get_slave_descr(void)
{
	return &mma845x_descr;
}
EXPORT_SYMBOL(mma845x_get_slave_descr);

/**
 *  @}
 */