Skip to content

Commit 5db5490

Browse files
liuyx1314rkhuangtao
authored andcommitted
drivers: input: sensors: update accel sensor mma7660 driver
Change-Id: Id3cf0cf3a14a07010524d0de58dc523bfcb3b735 Signed-off-by: Zorro Liu <lyx@rock-chips.com>
1 parent 2a8ce27 commit 5db5490

1 file changed

Lines changed: 88 additions & 117 deletions

File tree

drivers/input/sensors/accel/mma7660.c

Lines changed: 88 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -31,129 +31,95 @@
3131
#endif
3232
#include <linux/sensor-dev.h>
3333

34-
35-
#define MMA7660_ENABLE 1
36-
37-
#define MMA7660_REG_X_OUT 0x0
38-
#define MMA7660_REG_Y_OUT 0x1
39-
#define MMA7660_REG_Z_OUT 0x2
40-
#define MMA7660_REG_TILT 0x3
41-
#define MMA7660_REG_SRST 0x4
42-
#define MMA7660_REG_SPCNT 0x5
43-
#define MMA7660_REG_INTSU 0x6
44-
#define MMA7660_REG_MODE 0x7
45-
#define MMA7660_REG_SR 0x8
46-
#define MMA7660_REG_PDET 0x9
47-
#define MMA7660_REG_PD 0xa
48-
49-
50-
#define MMA7660_RANGE 1500000
51-
52-
/* LIS3DH */
53-
#define MMA7660_PRECISION 6
54-
#define MMA7660_BOUNDARY (0x1 << (MMA7660_PRECISION - 1))
55-
#define MMA7660_GRAVITY_STEP (MMA7660_RANGE / MMA7660_BOUNDARY)
56-
57-
#define MMA7660_COUNT_AVERAGE 2
58-
59-
struct sensor_axis_average {
60-
int x_average;
61-
int y_average;
62-
int z_average;
63-
int count;
64-
};
65-
66-
static struct sensor_axis_average axis_average;
34+
#define MMA7660_ENABLE 1
35+
#define MMA7660_REG_X_OUT 0x0
36+
#define MMA7660_REG_Y_OUT 0x1
37+
#define MMA7660_REG_Z_OUT 0x2
38+
#define MMA7660_REG_TILT 0x3
39+
#define MMA7660_REG_SRST 0x4
40+
#define MMA7660_REG_SPCNT 0x5
41+
#define MMA7660_REG_INTSU 0x6
42+
#define MMA7660_REG_MODE 0x7
43+
#define MMA7660_REG_SR 0x8
44+
#define MMA7660_REG_PDET 0x9
45+
#define MMA7660_REG_PD 0xa
46+
#define MMA7660_PRECISION 6
6747

6848
/****************operate according to sensor chip:start************/
6949

7050
static int sensor_active(struct i2c_client *client, int enable, int rate)
7151
{
7252
struct sensor_private_data *sensor =
73-
(struct sensor_private_data *) i2c_get_clientdata(client);
53+
(struct sensor_private_data *)i2c_get_clientdata(client);
7454
int result = 0;
7555
int status = 0;
76-
56+
7757
sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
78-
79-
//register setting according to chip datasheet
80-
if(enable)
81-
{
82-
status = MMA7660_ENABLE; //mma7660
58+
59+
if (enable) {
60+
status = MMA7660_ENABLE;
8361
sensor->ops->ctrl_data |= status;
84-
}
85-
else
86-
{
87-
status = ~MMA7660_ENABLE; //mma7660
62+
} else {
63+
status = ~MMA7660_ENABLE;
8864
sensor->ops->ctrl_data &= status;
8965
}
9066

91-
DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
9267
result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
93-
if(result)
94-
printk("%s:fail to active sensor\n",__func__);
95-
96-
return result;
68+
if (result)
69+
dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
9770

71+
return result;
9872
}
9973

10074
static int sensor_init(struct i2c_client *client)
10175
{
10276
struct sensor_private_data *sensor =
103-
(struct sensor_private_data *) i2c_get_clientdata(client);
77+
(struct sensor_private_data *)i2c_get_clientdata(client);
10478
int result = 0;
105-
106-
result = sensor->ops->active(client,0,0);
107-
if(result)
108-
{
109-
printk("%s:line=%d,error\n",__func__,__LINE__);
79+
80+
result = sensor->ops->active(client, 0, 0);
81+
if (result) {
82+
dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
11083
return result;
11184
}
112-
113-
sensor->status_cur = SENSOR_OFF;
11485

115-
DBG("%s:MMA7660_REG_TILT=0x%x\n",__func__,sensor_read_reg(client, MMA7660_REG_TILT));
86+
sensor->status_cur = SENSOR_OFF;
11687

117-
result = sensor_write_reg(client, MMA7660_REG_SR, (0x01<<5)| 0x02); //32 Samples/Second Active and Auto-Sleep Mode
118-
if(result)
119-
{
120-
printk("%s:line=%d,error\n",__func__,__LINE__);
88+
/*120 Samples/Second Active and Auto-Sleep Mode */
89+
result = sensor_write_reg(client, MMA7660_REG_SR, 0x01 << 5);
90+
if (result) {
91+
dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
12192
return result;
12293
}
12394

124-
if(sensor->pdata->irq_enable) //open interrupt
125-
{
126-
result = sensor_write_reg(client, MMA7660_REG_INTSU, 1<<4);//enable int,GINT=1
127-
if(result)
128-
{
129-
printk("%s:line=%d,error\n",__func__,__LINE__);
95+
if (sensor->pdata->irq_enable) {
96+
result = sensor_write_reg(client, MMA7660_REG_INTSU, 1 << 4);
97+
if (result) {
98+
dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
13099
return result;
131100
}
132101
}
133-
134-
sensor->ops->ctrl_data = 1<<6; //Interrupt output INT is push-pull
102+
103+
sensor->ops->ctrl_data = 1 << 6;
135104
result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
136-
if(result)
137-
{
138-
printk("%s:line=%d,error\n",__func__,__LINE__);
105+
if (result) {
106+
dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
139107
return result;
140108
}
141109

142-
143-
memset(&axis_average, 0, sizeof(struct sensor_axis_average));
144-
145110
return result;
146111
}
147112

148-
149113
static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
150114
{
151-
s64 result;
115+
int result = (int)low_byte;
152116

153-
result = (int)low_byte;
154-
result *= 768;
117+
if (low_byte & 0x20)
118+
result = ((~result & 0x1f) + 1) * (-768);
119+
else
120+
result = (result & 0x1f) * 768;
155121

156-
return (int)result;
122+
return result;
157123
}
158124

159125
static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
@@ -172,61 +138,71 @@ static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *a
172138
return 0;
173139
}
174140

175-
#define GSENSOR_MIN 2
176141
static int sensor_report_value(struct i2c_client *client)
177142
{
178143
struct sensor_private_data *sensor =
179-
(struct sensor_private_data *) i2c_get_clientdata(client);
144+
(struct sensor_private_data *)i2c_get_clientdata(client);
180145
struct sensor_platform_data *pdata = sensor->pdata;
181146
int ret = 0;
182-
int x,y,z;
147+
int x, y, z;
183148
struct sensor_axis axis;
184-
char buffer[3] = {0};
149+
char buffer[3] = {0};
185150
char value = 0;
186-
187-
if(sensor->ops->read_len < 3) //sensor->ops->read_len = 3
188-
{
189-
printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
151+
static int flag;
152+
153+
if (sensor->ops->read_len < 3) {
154+
dev_err(&client->dev, "%s:lenth is error,len=%d\n", __func__, sensor->ops->read_len);
190155
return -1;
191156
}
192-
157+
193158
memset(buffer, 0, 3);
194-
195-
/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
159+
160+
/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
196161
do {
197162
*buffer = sensor->ops->read_reg;
198163
ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
199164
if (ret < 0)
200-
return ret;
165+
return ret;
201166
} while (0);
202167

203-
204-
//this gsensor need 6 bytes buffer
205-
x = sensor_convert_data(sensor->client, 0, buffer[0]); //buffer[1]:high bit
168+
x = sensor_convert_data(sensor->client, 0, buffer[0]);
206169
y = sensor_convert_data(sensor->client, 0, buffer[1]);
207-
z = sensor_convert_data(sensor->client, 0, buffer[2]);
208-
209-
axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
210-
axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
211-
axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
170+
z = sensor_convert_data(sensor->client, 0, buffer[2]);
171+
172+
axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
173+
axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
174+
axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
175+
176+
/*
177+
*input dev will ignore report data if data value is the same with last_value,
178+
*sample rate will not enough by this way, so just avoid this case
179+
*/
180+
if ((sensor->axis.x == axis.x) && (sensor->axis.y == axis.y) && (sensor->axis.z == axis.z)) {
181+
if (flag) {
182+
flag = 0;
183+
axis.x += 1;
184+
axis.y += 1;
185+
axis.z += 1;
186+
} else {
187+
flag = 1;
188+
axis.x -= 1;
189+
axis.y -= 1;
190+
axis.z -= 1;
191+
}
192+
}
212193

213194
gsensor_report_value(client, &axis);
214195

215196
mutex_lock(&sensor->data_mutex);
216197
sensor->axis = axis;
217198
mutex_unlock(&sensor->data_mutex);
218199

219-
if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
220-
{
221-
200+
if (sensor->pdata->irq_enable && sensor->ops->int_status_reg >= 0)
222201
value = sensor_read_reg(client, sensor->ops->int_status_reg);
223-
DBG("%s:sensor int status :0x%x\n",__func__,value);
224-
}
225-
202+
226203
return ret;
227204
}
228205

229-
230206
struct sensor_operate gsensor_mma7660_ops = {
231207
.name = "mma7660",
232208
.type = SENSOR_TYPE_ACCEL,
@@ -238,7 +214,7 @@ struct sensor_operate gsensor_mma7660_ops = {
238214
.precision = MMA7660_PRECISION,
239215
.ctrl_reg = MMA7660_REG_MODE,
240216
.int_status_reg = SENSOR_UNKNOW_DATA,
241-
.range = {-24576, 24576},
217+
.range = {-24576, 24576},
242218
.trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
243219
.active = sensor_active,
244220
.init = sensor_init,
@@ -253,26 +229,21 @@ static struct sensor_operate *gsensor_get_ops(void)
253229
return &gsensor_mma7660_ops;
254230
}
255231

256-
257232
static int __init gsensor_mma7660_init(void)
258233
{
259234
struct sensor_operate *ops = gsensor_get_ops();
260-
int result = 0;
261235
int type = ops->type;
262-
result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
263-
return result;
236+
237+
return sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
264238
}
265239

266240
static void __exit gsensor_mma7660_exit(void)
267241
{
268242
struct sensor_operate *ops = gsensor_get_ops();
269243
int type = ops->type;
244+
270245
sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
271246
}
272247

273-
274248
module_init(gsensor_mma7660_init);
275249
module_exit(gsensor_mma7660_exit);
276-
277-
278-

0 commit comments

Comments
 (0)