mma8450.c
8.51 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Driver for Freescale's 3-Axis Accelerometer MMA8450
*
* Copyright (C) 2011-2014 Freescale Semiconductor, Inc. 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/input-polldev.h>
#include <linux/of_device.h>
#include <linux/mutex.h>
#define MMA8450_DRV_NAME "mma8450"
#define MODE_CHANGE_DELAY_MS 100
#define POLL_INTERVAL 100
#define POLL_INTERVAL_MAX 500
/* register definitions */
#define MMA8450_STATUS 0x00
#define MMA8450_STATUS_ZXYDR 0x08
#define MMA8450_OUT_X8 0x01
#define MMA8450_OUT_Y8 0x02
#define MMA8450_OUT_Z8 0x03
#define MMA8450_OUT_X_LSB 0x05
#define MMA8450_OUT_X_MSB 0x06
#define MMA8450_OUT_Y_LSB 0x07
#define MMA8450_OUT_Y_MSB 0x08
#define MMA8450_OUT_Z_LSB 0x09
#define MMA8450_OUT_Z_MSB 0x0a
#define MMA8450_XYZ_DATA_CFG 0x16
#define MMA8450_CTRL_REG1 0x38
#define MMA8450_CTRL_REG2 0x39
#define MMA8450_ID 0xC6
#define MMA8450_WHO_AM_I 0x0F
enum {
MODE_STANDBY = 0,
MODE_2G,
MODE_4G,
MODE_8G,
};
/* mma8450 status */
struct mma8450 {
struct i2c_client *client;
struct input_polled_dev *idev;
struct mutex mma8450_lock;
u8 mode;
};
static int mma8450_read(struct mma8450 *m, unsigned off)
{
struct i2c_client *c = m->client;
int ret;
ret = i2c_smbus_read_byte_data(c, off);
if (ret < 0)
dev_err(&c->dev,
"failed to read register 0x%02x, error %d\n",
off, ret);
return ret;
}
static int mma8450_write(struct mma8450 *m, unsigned off, u8 v)
{
struct i2c_client *c = m->client;
int error;
error = i2c_smbus_write_byte_data(c, off, v);
if (error < 0) {
dev_err(&c->dev,
"failed to write to register 0x%02x, error %d\n",
off, error);
return error;
}
return 0;
}
static int mma8450_read_block(struct mma8450 *m, unsigned off,
u8 *buf, size_t size)
{
struct i2c_client *c = m->client;
int err;
err = i2c_smbus_read_i2c_block_data(c, off, size, buf);
if (err < 0) {
dev_err(&c->dev,
"failed to read block data at 0x%02x, error %d\n",
MMA8450_OUT_X_LSB, err);
return err;
}
return 0;
}
static void mma8450_poll(struct input_polled_dev *dev)
{
struct mma8450 *m = dev->private;
int x, y, z;
int ret;
u8 buf[6];
mutex_lock(&m->mma8450_lock);
ret = mma8450_read(m, MMA8450_STATUS);
if (ret < 0 || !(ret & MMA8450_STATUS_ZXYDR)) {
mutex_unlock(&m->mma8450_lock);
return;
}
ret = mma8450_read_block(m, MMA8450_OUT_X_LSB, buf, sizeof(buf));
if (ret < 0) {
mutex_unlock(&m->mma8450_lock);
return;
}
x = ((int)(s8)buf[1] << 4) | (buf[0] & 0xf);
y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf);
z = ((int)(s8)buf[5] << 4) | (buf[4] & 0xf);
input_report_abs(dev->input, ABS_X, x);
input_report_abs(dev->input, ABS_Y, y);
input_report_abs(dev->input, ABS_Z, z);
input_sync(dev->input);
mutex_unlock(&m->mma8450_lock);
}
/* Initialize the MMA8450 chip */
static s32 mma8450_open(struct input_polled_dev *dev)
{
struct mma8450 *m = dev->private;
int err;
/* enable all events from X/Y/Z, no FIFO */
err = mma8450_write(m, MMA8450_XYZ_DATA_CFG, 0x07);
if (err)
return err;
/*
* Sleep mode poll rate - 50Hz
* System output data rate - 400Hz
* Standby mode
*/
err = mma8450_write(m, MMA8450_CTRL_REG1, MODE_STANDBY);
if (err)
return err;
m->mode = MODE_STANDBY;
msleep(MODE_CHANGE_DELAY_MS);
return 0;
}
static void mma8450_close(struct input_polled_dev *dev)
{
struct mma8450 *m = dev->private;
mma8450_write(m, MMA8450_CTRL_REG1, 0x00);
mma8450_write(m, MMA8450_CTRL_REG2, 0x01);
}
static ssize_t mma8450_scalemode_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int mode = 0;
struct mma8450 *m;
struct i2c_client *client = to_i2c_client(dev);
m = i2c_get_clientdata(client);
mutex_lock(&m->mma8450_lock);
mode = (int)m->mode;
mutex_unlock(&m->mma8450_lock);
return sprintf(buf, "%d\n", mode);
}
static ssize_t mma8450_scalemode_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned long mode;
int ret;
struct mma8450 *m = NULL;
struct i2c_client *client = to_i2c_client(dev);
ret = strict_strtoul(buf, 10, &mode);
if (ret) {
dev_err(dev, "string transform error\n");
return ret;
}
if (mode > MODE_8G) {
dev_warn(dev, "not supported mode %d\n", (int)mode);
return count;
}
m = i2c_get_clientdata(client);
mutex_lock(&m->mma8450_lock);
if (mode == m->mode) {
mutex_unlock(&m->mma8450_lock);
return count;
}
ret = mma8450_write(m, MMA8450_CTRL_REG1, mode);
if (ret < 0) {
mutex_unlock(&m->mma8450_lock);
return ret;
}
msleep(MODE_CHANGE_DELAY_MS);
m->mode = (u8)mode;
mutex_unlock(&m->mma8450_lock);
return count;
}
static DEVICE_ATTR(scalemode, S_IWUSR | S_IRUGO,
mma8450_scalemode_show, mma8450_scalemode_store);
static struct attribute *mma8450_attributes[] = {
&dev_attr_scalemode.attr,
NULL
};
static const struct attribute_group mma8450_attr_group = {
.attrs = mma8450_attributes,
};
/*
* I2C init/probing/exit functions
*/
static int mma8450_probe(struct i2c_client *c,
const struct i2c_device_id *id)
{
struct input_polled_dev *idev;
struct mma8450 *m;
int err, client_id;
struct i2c_adapter *adapter = NULL;
adapter = to_i2c_adapter(c->dev.parent);
err = i2c_check_functionality(adapter,
I2C_FUNC_SMBUS_BYTE |
I2C_FUNC_SMBUS_BYTE_DATA);
if (!err)
goto err_out;
client_id = i2c_smbus_read_byte_data(c, MMA8450_WHO_AM_I);
if (MMA8450_ID != client_id) {
dev_err(&c->dev,
"read chip ID 0x%x is not equal to 0x%x!\n", client_id,
MMA8450_ID);
err = -EINVAL;
goto err_out;
}
m = kzalloc(sizeof(struct mma8450), GFP_KERNEL);
idev = input_allocate_polled_device();
if (!m || !idev) {
err = -ENOMEM;
goto err_free_mem;
}
m->client = c;
m->idev = idev;
i2c_set_clientdata(c, m);
idev->private = m;
idev->input->name = MMA8450_DRV_NAME;
idev->input->id.bustype = BUS_I2C;
idev->poll = mma8450_poll;
idev->poll_interval = POLL_INTERVAL;
idev->poll_interval_max = POLL_INTERVAL_MAX;
__set_bit(EV_ABS, idev->input->evbit);
input_set_abs_params(idev->input, ABS_X, -2048, 2047, 32, 32);
input_set_abs_params(idev->input, ABS_Y, -2048, 2047, 32, 32);
input_set_abs_params(idev->input, ABS_Z, -2048, 2047, 32, 32);
err = input_register_polled_device(idev);
if (err) {
dev_err(&c->dev, "failed to register polled input device\n");
goto err_free_mem;
}
mutex_init(&m->mma8450_lock);
err = mma8450_open(idev);
if (err) {
dev_err(&c->dev, "failed to initialize mma8450\n");
goto err_unreg_dev;
}
err = sysfs_create_group(&c->dev.kobj, &mma8450_attr_group);
if (err) {
dev_err(&c->dev, "create device file failed!\n");
err = -EINVAL;
goto err_close;
}
return 0;
err_close:
mma8450_close(idev);
err_unreg_dev:
mutex_destroy(&m->mma8450_lock);
input_unregister_polled_device(idev);
err_free_mem:
input_free_polled_device(idev);
kfree(m);
err_out:
return err;
}
static int mma8450_remove(struct i2c_client *c)
{
struct mma8450 *m = i2c_get_clientdata(c);
struct input_polled_dev *idev = m->idev;
sysfs_remove_group(&c->dev.kobj, &mma8450_attr_group);
mma8450_close(idev);
mutex_destroy(&m->mma8450_lock);
input_unregister_polled_device(idev);
input_free_polled_device(idev);
kfree(m);
return 0;
}
static const struct i2c_device_id mma8450_id[] = {
{ MMA8450_DRV_NAME, 0 },
{ },
};
MODULE_DEVICE_TABLE(i2c, mma8450_id);
static const struct of_device_id mma8450_dt_ids[] = {
{ .compatible = "fsl,mma8450", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, mma8450_dt_ids);
static struct i2c_driver mma8450_driver = {
.driver = {
.name = MMA8450_DRV_NAME,
.owner = THIS_MODULE,
.of_match_table = mma8450_dt_ids,
},
.probe = mma8450_probe,
.remove = mma8450_remove,
.id_table = mma8450_id,
};
module_i2c_driver(mma8450_driver);
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("MMA8450 3-Axis Accelerometer Driver");
MODULE_LICENSE("GPL");